|
70 | 70 |
|
71 | 71 | <!-- 这里可写通用的实现逻辑 -->
|
72 | 72 |
|
73 |
| -状态压缩 + 前缀和 |
| 73 | +状态压缩 + 前缀和。 |
| 74 | + |
| 75 | +类似题目:[1371. 每个元音包含偶数次的最长子字符串](/solution/1300-1399/1371.Find%20the%20Longest%20Substring%20Containing%20Vowels%20in%20Even%20Counts/README.md) |
74 | 76 |
|
75 | 77 | <!-- tabs:start -->
|
76 | 78 |
|
|
79 | 81 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
80 | 82 |
|
81 | 83 | ```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 |
83 | 96 | ```
|
84 | 97 |
|
85 | 98 | ### **Java**
|
86 | 99 |
|
87 | 100 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
88 | 101 |
|
89 | 102 | ```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 | +} |
91 | 120 | ```
|
92 | 121 |
|
93 | 122 | ### **JavaScript**
|
|
98 | 127 | * @return {number}
|
99 | 128 | */
|
100 | 129 | 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; |
105 | 133 | let ans = 0;
|
106 | 134 | 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)]; |
112 | 139 | }
|
113 |
| - ++counts[pre]; |
| 140 | + ++counter[state]; |
114 | 141 | }
|
115 | 142 | return ans;
|
116 | 143 | };
|
117 | 144 | ```
|
118 | 145 |
|
| 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 | + |
119 | 188 | ### **...**
|
120 | 189 |
|
121 | 190 | ```
|
|
0 commit comments