|
58 | 58 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
59 | 59 |
|
60 | 60 | ```python
|
61 |
| - |
| 61 | +class Solution: |
| 62 | + def countCompleteSubstrings(self, word: str, k: int) -> int: |
| 63 | + def f(s: str) -> int: |
| 64 | + m = len(s) |
| 65 | + ans = 0 |
| 66 | + for i in range(1, 27): |
| 67 | + l = i * k |
| 68 | + if l > m: |
| 69 | + break |
| 70 | + cnt = Counter(s[:l]) |
| 71 | + freq = Counter(cnt.values()) |
| 72 | + ans += freq[k] == i |
| 73 | + for j in range(l, m): |
| 74 | + freq[cnt[s[j]]] -= 1 |
| 75 | + cnt[s[j]] += 1 |
| 76 | + freq[cnt[s[j]]] += 1 |
| 77 | + |
| 78 | + freq[cnt[s[j - l]]] -= 1 |
| 79 | + cnt[s[j - l]] -= 1 |
| 80 | + freq[cnt[s[j - l]]] += 1 |
| 81 | + |
| 82 | + ans += freq[k] == i |
| 83 | + return ans |
| 84 | + |
| 85 | + n = len(word) |
| 86 | + ans = i = 0 |
| 87 | + while i < n: |
| 88 | + j = i + 1 |
| 89 | + while j < n and abs(ord(word[j]) - ord(word[j - 1])) <= 2: |
| 90 | + j += 1 |
| 91 | + ans += f(word[i:j]) |
| 92 | + i = j |
| 93 | + return ans |
62 | 94 | ```
|
63 | 95 |
|
64 | 96 | ### **Java**
|
65 | 97 |
|
66 | 98 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
67 | 99 |
|
68 | 100 | ```java
|
69 |
| - |
| 101 | +class Solution { |
| 102 | + public int countCompleteSubstrings(String word, int k) { |
| 103 | + int n = word.length(); |
| 104 | + int ans = 0; |
| 105 | + for (int i = 0; i < n;) { |
| 106 | + int j = i + 1; |
| 107 | + while (j < n && Math.abs(word.charAt(j) - word.charAt(j - 1)) <= 2) { |
| 108 | + ++j; |
| 109 | + } |
| 110 | + ans += f(word.substring(i, j), k); |
| 111 | + i = j; |
| 112 | + } |
| 113 | + return ans; |
| 114 | + } |
| 115 | + |
| 116 | + private int f(String s, int k) { |
| 117 | + int m = s.length(); |
| 118 | + int ans = 0; |
| 119 | + for (int i = 1; i <= 26 && i * k <= m; ++i) { |
| 120 | + int l = i * k; |
| 121 | + int[] cnt = new int[26]; |
| 122 | + for (int j = 0; j < l; ++j) { |
| 123 | + ++cnt[s.charAt(j) - 'a']; |
| 124 | + } |
| 125 | + Map<Integer, Integer> freq = new HashMap<>(); |
| 126 | + for (int x : cnt) { |
| 127 | + if (x > 0) { |
| 128 | + freq.merge(x, 1, Integer::sum); |
| 129 | + } |
| 130 | + } |
| 131 | + if (freq.getOrDefault(k, 0) == i) { |
| 132 | + ++ans; |
| 133 | + } |
| 134 | + for (int j = l; j < m; ++j) { |
| 135 | + int a = s.charAt(j) - 'a'; |
| 136 | + int b = s.charAt(j - l) - 'a'; |
| 137 | + freq.merge(cnt[a], -1, Integer::sum); |
| 138 | + ++cnt[a]; |
| 139 | + freq.merge(cnt[a], 1, Integer::sum); |
| 140 | + |
| 141 | + freq.merge(cnt[b], -1, Integer::sum); |
| 142 | + --cnt[b]; |
| 143 | + freq.merge(cnt[b], 1, Integer::sum); |
| 144 | + if (freq.getOrDefault(k, 0) == i) { |
| 145 | + ++ans; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + return ans; |
| 150 | + } |
| 151 | +} |
70 | 152 | ```
|
71 | 153 |
|
72 | 154 | ### **C++**
|
73 | 155 |
|
74 | 156 | ```cpp
|
75 |
| - |
| 157 | +class Solution { |
| 158 | +public: |
| 159 | + int countCompleteSubstrings(string word, int k) { |
| 160 | + int n = word.length(); |
| 161 | + int ans = 0; |
| 162 | + auto f = [&](string s) { |
| 163 | + int m = s.length(); |
| 164 | + int ans = 0; |
| 165 | + for (int i = 1; i <= 26 && i * k <= m; ++i) { |
| 166 | + int l = i * k; |
| 167 | + int cnt[26]{}; |
| 168 | + for (int j = 0; j < l; ++j) { |
| 169 | + ++cnt[s[j] - 'a']; |
| 170 | + } |
| 171 | + unordered_map<int, int> freq; |
| 172 | + for (int x : cnt) { |
| 173 | + if (x > 0) { |
| 174 | + freq[x]++; |
| 175 | + } |
| 176 | + } |
| 177 | + if (freq[k] == i) { |
| 178 | + ++ans; |
| 179 | + } |
| 180 | + for (int j = l; j < m; ++j) { |
| 181 | + int a = s[j] - 'a'; |
| 182 | + int b = s[j - l] - 'a'; |
| 183 | + freq[cnt[a]]--; |
| 184 | + cnt[a]++; |
| 185 | + freq[cnt[a]]++; |
| 186 | + |
| 187 | + freq[cnt[b]]--; |
| 188 | + cnt[b]--; |
| 189 | + freq[cnt[b]]++; |
| 190 | + |
| 191 | + if (freq[k] == i) { |
| 192 | + ++ans; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + return ans; |
| 197 | + }; |
| 198 | + for (int i = 0; i < n;) { |
| 199 | + int j = i + 1; |
| 200 | + while (j < n && abs(word[j] - word[j - 1]) <= 2) { |
| 201 | + ++j; |
| 202 | + } |
| 203 | + ans += f(word.substr(i, j - i)); |
| 204 | + i = j; |
| 205 | + } |
| 206 | + return ans; |
| 207 | + } |
| 208 | +}; |
76 | 209 | ```
|
77 | 210 |
|
78 | 211 | ### **Go**
|
79 | 212 |
|
80 | 213 | ```go
|
| 214 | +func countCompleteSubstrings(word string, k int) (ans int) { |
| 215 | + n := len(word) |
| 216 | + f := func(s string) (ans int) { |
| 217 | + m := len(s) |
| 218 | + for i := 1; i <= 26 && i*k <= m; i++ { |
| 219 | + l := i * k |
| 220 | + cnt := [26]int{} |
| 221 | + for j := 0; j < l; j++ { |
| 222 | + cnt[int(s[j]-'a')]++ |
| 223 | + } |
| 224 | + freq := map[int]int{} |
| 225 | + for _, x := range cnt { |
| 226 | + if x > 0 { |
| 227 | + freq[x]++ |
| 228 | + } |
| 229 | + } |
| 230 | + if freq[k] == i { |
| 231 | + ans++ |
| 232 | + } |
| 233 | + for j := l; j < m; j++ { |
| 234 | + a := int(s[j] - 'a') |
| 235 | + b := int(s[j-l] - 'a') |
| 236 | + freq[cnt[a]]-- |
| 237 | + cnt[a]++ |
| 238 | + freq[cnt[a]]++ |
| 239 | + |
| 240 | + freq[cnt[b]]-- |
| 241 | + cnt[b]-- |
| 242 | + freq[cnt[b]]++ |
| 243 | + |
| 244 | + if freq[k] == i { |
| 245 | + ans++ |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + return |
| 250 | + } |
| 251 | + for i := 0; i < n; { |
| 252 | + j := i + 1 |
| 253 | + for j < n && abs(int(word[j])-int(word[j-1])) <= 2 { |
| 254 | + j++ |
| 255 | + } |
| 256 | + ans += f(word[i:j]) |
| 257 | + i = j |
| 258 | + } |
| 259 | + return |
| 260 | +} |
| 261 | + |
| 262 | +func abs(x int) int { |
| 263 | + if x < 0 { |
| 264 | + return -x |
| 265 | + } |
| 266 | + return x |
| 267 | +} |
| 268 | +``` |
81 | 269 |
|
| 270 | +### **TypeScript** |
| 271 | + |
| 272 | +```ts |
| 273 | +function countCompleteSubstrings(word: string, k: number): number { |
| 274 | + const f = (s: string): number => { |
| 275 | + const m = s.length; |
| 276 | + let ans = 0; |
| 277 | + for (let i = 1; i <= 26 && i * k <= m; i++) { |
| 278 | + const l = i * k; |
| 279 | + const cnt: number[] = new Array(26).fill(0); |
| 280 | + for (let j = 0; j < l; j++) { |
| 281 | + cnt[s.charCodeAt(j) - 'a'.charCodeAt(0)]++; |
| 282 | + } |
| 283 | + const freq: { [key: number]: number } = {}; |
| 284 | + for (const x of cnt) { |
| 285 | + if (x > 0) { |
| 286 | + freq[x] = (freq[x] || 0) + 1; |
| 287 | + } |
| 288 | + } |
| 289 | + if (freq[k] === i) { |
| 290 | + ans++; |
| 291 | + } |
| 292 | + |
| 293 | + for (let j = l; j < m; j++) { |
| 294 | + const a = s.charCodeAt(j) - 'a'.charCodeAt(0); |
| 295 | + const b = s.charCodeAt(j - l) - 'a'.charCodeAt(0); |
| 296 | + |
| 297 | + freq[cnt[a]]--; |
| 298 | + cnt[a]++; |
| 299 | + freq[cnt[a]] = (freq[cnt[a]] || 0) + 1; |
| 300 | + |
| 301 | + freq[cnt[b]]--; |
| 302 | + cnt[b]--; |
| 303 | + freq[cnt[b]] = (freq[cnt[b]] || 0) + 1; |
| 304 | + |
| 305 | + if (freq[k] === i) { |
| 306 | + ans++; |
| 307 | + } |
| 308 | + } |
| 309 | + } |
| 310 | + |
| 311 | + return ans; |
| 312 | + }; |
| 313 | + |
| 314 | + let n = word.length; |
| 315 | + let ans = 0; |
| 316 | + for (let i = 0; i < n; ) { |
| 317 | + let j = i + 1; |
| 318 | + while (j < n && Math.abs(word.charCodeAt(j) - word.charCodeAt(j - 1)) <= 2) { |
| 319 | + j++; |
| 320 | + } |
| 321 | + ans += f(word.substring(i, j)); |
| 322 | + i = j; |
| 323 | + } |
| 324 | + return ans; |
| 325 | +} |
82 | 326 | ```
|
83 | 327 |
|
84 | 328 | ### **...**
|
|
0 commit comments