Skip to content

Commit f830be9

Browse files
committed
Add solution and test-cases for problem 1915
1 parent 09a1dcb commit f830be9

File tree

3 files changed

+61
-22
lines changed

3 files changed

+61
-22
lines changed

leetcode/1901-2000/1915.Number-of-Wonderful-Substrings/README.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,52 @@
11
# [1915.Number of Wonderful Substrings][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+
A **wonderful** string is a string where **at most one** letter appears an **odd** number of times.
5+
6+
- For example, `"ccjjc"` and `"abab"` are wonderful, but `"ab"` is not.
7+
8+
Given a string `word` that consists of the first ten lowercase English letters (`'a'` through `'j'`), return the **number of wonderful non-empty substrings** in `word`. If the same substring appears multiple times in word, then count **each occurrence** separately.
9+
10+
A **substring** is a contiguous sequence of characters in a string.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: word = "aba"
16+
Output: 4
17+
Explanation: The four wonderful substrings are underlined below:
18+
- "aba" -> "a"
19+
- "aba" -> "b"
20+
- "aba" -> "a"
21+
- "aba" -> "aba"
1322
```
1423

15-
## 题意
16-
> ...
17-
18-
## 题解
24+
**Example 2:**
1925

20-
### 思路1
21-
> ...
22-
Number of Wonderful Substrings
23-
```go
2426
```
27+
Input: word = "aabb"
28+
Output: 9
29+
Explanation: The nine wonderful substrings are underlined below:
30+
- "aabb" -> "a"
31+
- "aabb" -> "aa"
32+
- "aabb" -> "aab"
33+
- "aabb" -> "aabb"
34+
- "aabb" -> "a"
35+
- "aabb" -> "abb"
36+
- "aabb" -> "b"
37+
- "aabb" -> "bb"
38+
- "aabb" -> "b"
39+
```
40+
41+
**Example 3:**
2542

43+
```
44+
Input: word = "he"
45+
Output: 2
46+
Explanation: The two wonderful substrings are underlined below:
47+
- "he" -> "h"
48+
- "he" -> "e"
49+
```
2650

2751
## 结语
2852

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(word string) int64 {
4+
// 10bit
5+
ans := int64(0)
6+
l := len(word)
7+
state := make(map[int]int64)
8+
state[0] = 1
9+
mask := 0
10+
for i := 0; i < l; i++ {
11+
mask ^= (1 << int(word[i]-'a'))
12+
c := state[mask]
13+
ans += c
14+
state[mask] = c + 1
15+
for i := 0; i < 10; i++ {
16+
ans += state[mask^(1<<i)]
17+
}
18+
}
19+
return ans
520
}

leetcode/1901-2000/1915.Number-of-Wonderful-Substrings/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 int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "aba", 4},
17+
{"TestCase2", "aabb", 9},
18+
{"TestCase3", "he", 2},
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)