Skip to content

Commit

Permalink
Merge pull request #864 from 0xff-dev/1542
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1542
  • Loading branch information
6boris committed Jun 6, 2024
2 parents b6c05a0 + 67b464a commit 6820b44
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
30 changes: 17 additions & 13 deletions leetcode/1501-1600/1542.Find-Longest-Awesome-Substring/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
# [1542.Find Longest Awesome Substring][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
You are given a string `s`. An **awesome** substring is a non-empty substring of `s` such that we can make any number of swaps in order to make it a palindrome.

Return the length of the maximum length **awesome substring** of `s`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Find Longest Awesome Substring
```go
```
Input: s = "12345678"
Output: 1
```

**Example 3:**

```
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
```

## 结语

Expand Down
24 changes: 22 additions & 2 deletions leetcode/1501-1600/1542.Find-Longest-Awesome-Substring/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string) int {
state := map[int]int{
0: -1,
}

mask := 0
ans := 0
for i := 0; i < len(s); i++ {
shift := s[i] - '0'
mask ^= (1 << shift)
if v, ok := state[mask]; ok {
ans = max(ans, i-v)
} else {
state[mask] = i
}
for j := 0; j < 10; j++ {
if v, ok := state[mask^(1<<j)]; ok {
ans = max(ans, i-v)
}
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "3242415", 5},
{"TestCase2", "12345678", 1},
{"TestCase3", "213123", 6},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 6820b44

Please sign in to comment.