Skip to content

Commit 67b464a

Browse files
committed
Add solution and test-cases for problem 1542
1 parent 09a1dcb commit 67b464a

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

leetcode/1501-1600/1542.Find-Longest-Awesome-Substring/README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [1542.Find Longest Awesome Substring][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
Return the length of the maximum length **awesome substring** of `s`.
77

88
**Example 1:**
99

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

15-
## 题意
16-
> ...
16+
**Example 2:**
1717

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Find Longest Awesome Substring
23-
```go
18+
```
19+
Input: s = "12345678"
20+
Output: 1
2421
```
2522

23+
**Example 3:**
24+
25+
```
26+
Input: s = "213123"
27+
Output: 6
28+
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
29+
```
2630

2731
## 结语
2832

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) int {
4+
state := map[int]int{
5+
0: -1,
6+
}
7+
8+
mask := 0
9+
ans := 0
10+
for i := 0; i < len(s); i++ {
11+
shift := s[i] - '0'
12+
mask ^= (1 << shift)
13+
if v, ok := state[mask]; ok {
14+
ans = max(ans, i-v)
15+
} else {
16+
state[mask] = i
17+
}
18+
for j := 0; j < 10; j++ {
19+
if v, ok := state[mask^(1<<j)]; ok {
20+
ans = max(ans, i-v)
21+
}
22+
}
23+
}
24+
return ans
525
}

leetcode/1501-1600/1542.Find-Longest-Awesome-Substring/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "3242415", 5},
17+
{"TestCase2", "12345678", 1},
18+
{"TestCase3", "213123", 6},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)