Skip to content

Commit 6d42378

Browse files
authored
feat: add solutions for lc No.1915 (#5194)
1 parent ce9edb1 commit 6d42378

4 files changed

Lines changed: 83 additions & 10 deletions

File tree

solution/1900-1999/1915.Number of Wonderful Substrings/README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ tags:
108108
```python
109109
class Solution:
110110
def wonderfulSubstrings(self, word: str) -> int:
111-
cnt = Counter({0: 1})
111+
cnt = defaultdict(int)
112+
cnt[0] = 1
112113
ans = st = 0
113114
for c in word:
114115
st ^= 1 << (ord(c) - ord("a"))
115116
ans += cnt[st]
116-
for i in range(10):
117-
ans += cnt[st ^ (1 << i)]
117+
ans += sum(cnt[st ^ (1 << i)] for i in range(10))
118118
cnt[st] += 1
119119
return ans
120120
```
@@ -201,6 +201,28 @@ function wonderfulSubstrings(word: string): number {
201201
}
202202
```
203203

204+
#### Rust
205+
206+
```rust
207+
impl Solution {
208+
pub fn wonderful_substrings(word: String) -> i64 {
209+
let mut cnt = [0i64; 1 << 10];
210+
cnt[0] = 1;
211+
let mut ans: i64 = 0;
212+
let mut st: usize = 0;
213+
for c in word.chars() {
214+
st ^= 1 << (c as usize - 'a' as usize);
215+
ans += cnt[st];
216+
for i in 0..10 {
217+
ans += cnt[st ^ (1 << i)];
218+
}
219+
cnt[st] += 1;
220+
}
221+
ans
222+
}
223+
}
224+
```
225+
204226
#### JavaScript
205227

206228
```js

solution/1900-1999/1915.Number of Wonderful Substrings/README_EN.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,19 @@ tags:
119119

120120
<!-- solution:start -->
121121

122-
### Solution 1
122+
### Solution 1: Prefix XOR + Counting
123+
124+
Since the string contains only $10$ lowercase letters, we can use a $10$-bit integer to represent the parity of each letter count in the current prefix. The $i$-th bit is $1$ if the $i$-th letter appears an odd number of times, and $0$ if it appears an even number of times.
125+
126+
We iterate through each character in the string. Use a variable $st$ to maintain the current prefix XOR state, and an array $cnt$ to record how many times each prefix state has appeared. Initially, $st = 0$ and $cnt[0] = 1$.
127+
128+
For each character, update the prefix XOR state. If the current state has appeared $cnt[st]$ times, it means there are $cnt[st]$ substrings in which all letter counts are even, so we add $cnt[st]$ to the answer. In addition, for $0 \le i < 10$, toggling the $i$-th bit of $st$ (i.e., $st \oplus (1 << i)$) represents substrings where exactly one letter has an odd count, so we add $cnt[st \oplus (1 << i)]$ to the answer. Finally, increment the occurrence count of $st$ by $1$, and continue.
129+
130+
The time complexity is $O(n \times \Sigma)$, and the space complexity is $O(2^{\Sigma})$, where $\Sigma = 10$ and $n$ is the length of the string.
131+
132+
Similar problems:
133+
134+
- [1371. 每个元音包含偶数次的最长子字符串](https://github.com/doocs/leetcode/blob/main/solution/1300-1399/1371.Find%20the%20Longest%20Substring%20Containing%20Vowels%20in%20Even%20Counts/README_EN.md)
123135

124136
<!-- tabs:start -->
125137

@@ -128,13 +140,13 @@ tags:
128140
```python
129141
class Solution:
130142
def wonderfulSubstrings(self, word: str) -> int:
131-
cnt = Counter({0: 1})
143+
cnt = defaultdict(int)
144+
cnt[0] = 1
132145
ans = st = 0
133146
for c in word:
134147
st ^= 1 << (ord(c) - ord("a"))
135148
ans += cnt[st]
136-
for i in range(10):
137-
ans += cnt[st ^ (1 << i)]
149+
ans += sum(cnt[st ^ (1 << i)] for i in range(10))
138150
cnt[st] += 1
139151
return ans
140152
```
@@ -221,6 +233,28 @@ function wonderfulSubstrings(word: string): number {
221233
}
222234
```
223235

236+
#### Rust
237+
238+
```rust
239+
impl Solution {
240+
pub fn wonderful_substrings(word: String) -> i64 {
241+
let mut cnt = [0i64; 1 << 10];
242+
cnt[0] = 1;
243+
let mut ans: i64 = 0;
244+
let mut st: usize = 0;
245+
for c in word.chars() {
246+
st ^= 1 << (c as usize - 'a' as usize);
247+
ans += cnt[st];
248+
for i in 0..10 {
249+
ans += cnt[st ^ (1 << i)];
250+
}
251+
cnt[st] += 1;
252+
}
253+
ans
254+
}
255+
}
256+
```
257+
224258
#### JavaScript
225259

226260
```js
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution:
22
def wonderfulSubstrings(self, word: str) -> int:
3-
cnt = Counter({0: 1})
3+
cnt = defaultdict(int)
4+
cnt[0] = 1
45
ans = st = 0
56
for c in word:
67
st ^= 1 << (ord(c) - ord("a"))
78
ans += cnt[st]
8-
for i in range(10):
9-
ans += cnt[st ^ (1 << i)]
9+
ans += sum(cnt[st ^ (1 << i)] for i in range(10))
1010
cnt[st] += 1
1111
return ans
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn wonderful_substrings(word: String) -> i64 {
3+
let mut cnt = [0i64; 1 << 10];
4+
cnt[0] = 1;
5+
let mut ans: i64 = 0;
6+
let mut st: usize = 0;
7+
for c in word.chars() {
8+
st ^= 1 << (c as usize - 'a' as usize);
9+
ans += cnt[st];
10+
for i in 0..10 {
11+
ans += cnt[st ^ (1 << i)];
12+
}
13+
cnt[st] += 1;
14+
}
15+
ans
16+
}
17+
}

0 commit comments

Comments
 (0)