Skip to content

Commit 788acd2

Browse files
committed
feat: add solutions to lc problem: No.1915
No.1915. Number of Wonderful Substrings
1 parent c689618 commit 788acd2

File tree

8 files changed

+232
-35
lines changed

8 files changed

+232
-35
lines changed

solution/1300-1399/1371.Find the Longest Substring Containing Vowels in Even Counts/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949

5050
前缀异或 + 状态压缩。
5151

52+
类似题目:[1915. 最美子字符串的数目](/solution/1900-1999/1915.Number%20of%20Wonderful%20Substrings/README.md)
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**

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

+82-13
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@
7070

7171
<!-- 这里可写通用的实现逻辑 -->
7272

73-
状态压缩 + 前缀和
73+
状态压缩 + 前缀和。
74+
75+
类似题目:[1371. 每个元音包含偶数次的最长子字符串](/solution/1300-1399/1371.Find%20the%20Longest%20Substring%20Containing%20Vowels%20in%20Even%20Counts/README.md)
7476

7577
<!-- tabs:start -->
7678

@@ -79,15 +81,42 @@
7981
<!-- 这里可写当前语言的特殊实现逻辑 -->
8082

8183
```python
82-
84+
class Solution:
85+
def wonderfulSubstrings(self, word: str) -> int:
86+
counter = Counter({0: 1})
87+
state = 0
88+
ans = 0
89+
for c in word:
90+
state ^= (1 << (ord(c) - ord('a')))
91+
ans += counter[state]
92+
for i in range(10):
93+
ans += counter[state ^ (1 << i)]
94+
counter[state] += 1
95+
return ans
8396
```
8497

8598
### **Java**
8699

87100
<!-- 这里可写当前语言的特殊实现逻辑 -->
88101

89102
```java
90-
103+
class Solution {
104+
public long wonderfulSubstrings(String word) {
105+
int[] counter = new int[1 << 10];
106+
counter[0] = 1;
107+
int state = 0;
108+
long ans = 0;
109+
for (char c : word.toCharArray()) {
110+
state ^= (1 << (c - 'a'));
111+
ans += counter[state];
112+
for (int i = 0; i < 10; ++i) {
113+
ans += counter[state ^ (1 << i)];
114+
}
115+
++counter[state];
116+
}
117+
return ans;
118+
}
119+
}
91120
```
92121

93122
### **JavaScript**
@@ -98,24 +127,64 @@
98127
* @return {number}
99128
*/
100129
var wonderfulSubstrings = function (word) {
101-
let n = 1 << 10;
102-
let counts = new Array(n).fill(0);
103-
counts[0] = 1;
104-
let pre = 0;
130+
let counter = new Array(1 << 10).fill(0);
131+
counter[0] = 1;
132+
let state = 0;
105133
let ans = 0;
106134
for (let c of word) {
107-
let cur = c.charCodeAt(0) - "a".charCodeAt(0);
108-
pre ^= 1 << cur;
109-
ans += counts[pre];
110-
for (let i = 1; i < n; i <<= 1) {
111-
ans += counts[pre ^ i];
135+
state ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
136+
ans += counter[state];
137+
for (let i = 0; i < 10; ++i) {
138+
ans += counter[state ^ (1 << i)];
112139
}
113-
++counts[pre];
140+
++counter[state];
114141
}
115142
return ans;
116143
};
117144
```
118145

146+
### **C++**
147+
148+
```cpp
149+
class Solution {
150+
public:
151+
long long wonderfulSubstrings(string word) {
152+
vector<int> counter(1024);
153+
counter[0] = 1;
154+
long long ans = 0;
155+
int state = 0;
156+
for (char c : word)
157+
{
158+
state ^= (1 << (c - 'a'));
159+
ans += counter[state];
160+
for (int i = 0; i < 10; ++i) ans += counter[state ^ (1 << i)];
161+
++counter[state];
162+
}
163+
return ans;
164+
}
165+
};
166+
```
167+
168+
### **Go**
169+
170+
```go
171+
func wonderfulSubstrings(word string) int64 {
172+
counter := make([]int, 1024)
173+
counter[0] = 1
174+
state := 0
175+
var ans int64
176+
for _, c := range word {
177+
state ^= (1 << (c - 'a'))
178+
ans += int64(counter[state])
179+
for i := 0; i < 10; i++ {
180+
ans += int64(counter[state^(1<<i)])
181+
}
182+
counter[state]++
183+
}
184+
return ans
185+
}
186+
```
187+
119188
### **...**
120189

121190
```

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

+79-12
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,40 @@
6969
### **Python3**
7070

7171
```python
72-
72+
class Solution:
73+
def wonderfulSubstrings(self, word: str) -> int:
74+
counter = Counter({0: 1})
75+
state = 0
76+
ans = 0
77+
for c in word:
78+
state ^= (1 << (ord(c) - ord('a')))
79+
ans += counter[state]
80+
for i in range(10):
81+
ans += counter[state ^ (1 << i)]
82+
counter[state] += 1
83+
return ans
7384
```
7485

7586
### **Java**
7687

7788
```java
78-
89+
class Solution {
90+
public long wonderfulSubstrings(String word) {
91+
int[] counter = new int[1 << 10];
92+
counter[0] = 1;
93+
int state = 0;
94+
long ans = 0;
95+
for (char c : word.toCharArray()) {
96+
state ^= (1 << (c - 'a'));
97+
ans += counter[state];
98+
for (int i = 0; i < 10; ++i) {
99+
ans += counter[state ^ (1 << i)];
100+
}
101+
++counter[state];
102+
}
103+
return ans;
104+
}
105+
}
79106
```
80107

81108
### **JavaScript**
@@ -86,24 +113,64 @@
86113
* @return {number}
87114
*/
88115
var wonderfulSubstrings = function (word) {
89-
let n = 1 << 10;
90-
let counts = new Array(n).fill(0);
91-
counts[0] = 1;
92-
let pre = 0;
116+
let counter = new Array(1 << 10).fill(0);
117+
counter[0] = 1;
118+
let state = 0;
93119
let ans = 0;
94120
for (let c of word) {
95-
let cur = c.charCodeAt(0) - "a".charCodeAt(0);
96-
pre ^= 1 << cur;
97-
ans += counts[pre];
98-
for (let i = 1; i < n; i <<= 1) {
99-
ans += counts[pre ^ i];
121+
state ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
122+
ans += counter[state];
123+
for (let i = 0; i < 10; ++i) {
124+
ans += counter[state ^ (1 << i)];
100125
}
101-
++counts[pre];
126+
++counter[state];
102127
}
103128
return ans;
104129
};
105130
```
106131

132+
### **C++**
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
long long wonderfulSubstrings(string word) {
138+
vector<int> counter(1024);
139+
counter[0] = 1;
140+
long long ans = 0;
141+
int state = 0;
142+
for (char c : word)
143+
{
144+
state ^= (1 << (c - 'a'));
145+
ans += counter[state];
146+
for (int i = 0; i < 10; ++i) ans += counter[state ^ (1 << i)];
147+
++counter[state];
148+
}
149+
return ans;
150+
}
151+
};
152+
```
153+
154+
### **Go**
155+
156+
```go
157+
func wonderfulSubstrings(word string) int64 {
158+
counter := make([]int, 1024)
159+
counter[0] = 1
160+
state := 0
161+
var ans int64
162+
for _, c := range word {
163+
state ^= (1 << (c - 'a'))
164+
ans += int64(counter[state])
165+
for i := 0; i < 10; i++ {
166+
ans += int64(counter[state^(1<<i)])
167+
}
168+
counter[state]++
169+
}
170+
return ans
171+
}
172+
```
173+
107174
### **...**
108175

109176
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
long long wonderfulSubstrings(string word) {
4+
vector<int> counter(1024);
5+
counter[0] = 1;
6+
long long ans = 0;
7+
int state = 0;
8+
for (char c : word)
9+
{
10+
state ^= (1 << (c - 'a'));
11+
ans += counter[state];
12+
for (int i = 0; i < 10; ++i) ans += counter[state ^ (1 << i)];
13+
++counter[state];
14+
}
15+
return ans;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func wonderfulSubstrings(word string) int64 {
2+
counter := make([]int, 1024)
3+
counter[0] = 1
4+
state := 0
5+
var ans int64
6+
for _, c := range word {
7+
state ^= (1 << (c - 'a'))
8+
ans += int64(counter[state])
9+
for i := 0; i < 10; i++ {
10+
ans += int64(counter[state^(1<<i)])
11+
}
12+
counter[state]++
13+
}
14+
return ans
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public long wonderfulSubstrings(String word) {
3+
int[] counter = new int[1 << 10];
4+
counter[0] = 1;
5+
int state = 0;
6+
long ans = 0;
7+
for (char c : word.toCharArray()) {
8+
state ^= (1 << (c - 'a'));
9+
ans += counter[state];
10+
for (int i = 0; i < 10; ++i) {
11+
ans += counter[state ^ (1 << i)];
12+
}
13+
++counter[state];
14+
}
15+
return ans;
16+
}
17+
}

solution/1900-1999/1915.Number of Wonderful Substrings/Solution.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
* @return {number}
44
*/
55
var wonderfulSubstrings = function (word) {
6-
let n = 1 << 10;
7-
let counts = new Array(n).fill(0);
8-
counts[0] = 1;
9-
let pre = 0;
6+
let counter = new Array(1 << 10).fill(0);
7+
counter[0] = 1;
8+
let state = 0;
109
let ans = 0;
1110
for (let c of word) {
12-
let cur = c.charCodeAt(0) - "a".charCodeAt(0);
13-
pre ^= 1 << cur;
14-
ans += counts[pre];
15-
for (let i = 1; i < n; i <<= 1) {
16-
ans += counts[pre ^ i];
11+
state ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
12+
ans += counter[state];
13+
for (let i = 0; i < 10; ++i) {
14+
ans += counter[state ^ (1 << i)];
1715
}
18-
++counts[pre];
16+
++counter[state];
1917
}
2018
return ans;
2119
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def wonderfulSubstrings(self, word: str) -> int:
3+
counter = Counter({0: 1})
4+
state = 0
5+
ans = 0
6+
for c in word:
7+
state ^= (1 << (ord(c) - ord('a')))
8+
ans += counter[state]
9+
for i in range(10):
10+
ans += counter[state ^ (1 << i)]
11+
counter[state] += 1
12+
return ans

0 commit comments

Comments
 (0)