From da5a858838c011eb31a9dd104da3d5df1b745d5a Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 11 Apr 2024 18:35:48 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.2067 No.2067.Number of Equal Count Substrings --- .../README.md | 176 +++++++++-------- .../README_EN.md | 184 +++++++++--------- .../Solution.cpp | 29 ++- .../Solution.go | 29 ++- .../Solution.java | 38 ++-- .../Solution.js | 31 ++- .../Solution.py | 25 ++- .../Solution.ts | 21 ++ 8 files changed, 274 insertions(+), 259 deletions(-) create mode 100644 solution/2000-2099/2067.Number of Equal Count Substrings/Solution.ts diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/README.md b/solution/2000-2099/2067.Number of Equal Count Substrings/README.md index 4d2ea9ba81dd1..1edd1c5999514 100644 --- a/solution/2000-2099/2067.Number of Equal Count Substrings/README.md +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/README.md @@ -75,22 +75,21 @@ class Solution: def equalCountSubstrings(self, s: str, count: int) -> int: ans = 0 - for x in range(1, 27): - m = count * x - if m > len(s): + for i in range(1, 27): + k = i * count + if k > len(s): break cnt = Counter() - y = 0 - for i, c in enumerate(s): + t = 0 + for j, c in enumerate(s): cnt[c] += 1 - y += cnt[c] == count - y -= cnt[c] == count + 1 - j = i - m - if j >= 0: - cnt[s[j]] -= 1 - y += cnt[s[j]] == count - y -= cnt[s[j]] == count - 1 - ans += x == y + t += cnt[c] == count + t -= cnt[c] == count + 1 + if j >= k: + cnt[s[j - k]] -= 1 + t += cnt[s[j - k]] == count + t -= cnt[s[j - k]] == count - 1 + ans += i == t return ans ``` @@ -98,34 +97,24 @@ class Solution: class Solution { public int equalCountSubstrings(String s, int count) { int ans = 0; + int[] cnt = new int[26]; int n = s.length(); - for (int x = 1; x < 27 && count * x <= n; ++x) { - int m = count * x; - int[] cnt = new int[26]; - int y = 0; - for (int i = 0; i < n; ++i) { - int a = s.charAt(i) - 'a'; + for (int i = 1; i < 27 && i * count <= n; ++i) { + int k = i * count; + Arrays.fill(cnt, 0); + int t = 0; + for (int j = 0; j < n; ++j) { + int a = s.charAt(j) - 'a'; ++cnt[a]; - if (cnt[a] == count) { - ++y; - } - if (cnt[a] == count + 1) { - --y; - } - int j = i - m; - if (j >= 0) { - int b = s.charAt(j) - 'a'; + t += cnt[a] == count ? 1 : 0; + t -= cnt[a] == count + 1 ? 1 : 0; + if (j - k >= 0) { + int b = s.charAt(j - k) - 'a'; --cnt[b]; - if (cnt[b] == count) { - ++y; - } - if (cnt[b] == count - 1) { - --y; - } - } - if (x == y) { - ++ans; + t += cnt[b] == count ? 1 : 0; + t -= cnt[b] == count - 1 ? 1 : 0; } + ans += i == t ? 1 : 0; } } return ans; @@ -140,23 +129,20 @@ public: int ans = 0; int n = s.size(); int cnt[26]; - for (int x = 1; x < 27 && count * x <= n; ++x) { - int m = count * x; - memset(cnt, 0, sizeof cnt); - int y = 0; - for (int i = 0; i < n; ++i) { - int a = s[i] - 'a'; - ++cnt[a]; - y += cnt[a] == count; - y -= cnt[a] == count + 1; - int j = i - m; - if (j >= 0) { - int b = s[j] - 'a'; - --cnt[b]; - y += cnt[b] == count; - y -= cnt[b] == count - 1; + for (int i = 1; i < 27 && i * count <= n; ++i) { + int k = i * count; + memset(cnt, 0, sizeof(cnt)); + int t = 0; + for (int j = 0; j < n; ++j) { + int a = s[j] - 'a'; + t += ++cnt[a] == count; + t -= cnt[a] == count + 1; + if (j >= k) { + int b = s[j - k] - 'a'; + t += --cnt[b] == count; + t -= cnt[b] == count - 1; } - ans += x == y; + ans += i == t; } } return ans; @@ -167,31 +153,28 @@ public: ```go func equalCountSubstrings(s string, count int) (ans int) { n := len(s) - for x := 1; x < 27 && x*count <= n; x++ { - m := x * count - y := 0 + for i := 1; i < 27 && i*count <= n; i++ { + k := i * count cnt := [26]int{} - for i, c := range s { + t := 0 + for j, c := range s { a := c - 'a' cnt[a]++ if cnt[a] == count { - y++ + t++ + } else if cnt[a] == count+1 { + t-- } - if cnt[a] == count+1 { - y-- - } - j := i - m - if j >= 0 { - b := s[j] - 'a' + if j >= k { + b := s[j-k] - 'a' cnt[b]-- if cnt[b] == count { - y++ - } - if cnt[b] == count-1 { - y-- + t++ + } else if cnt[b] == count-1 { + t-- } } - if x == y { + if i == t { ans++ } } @@ -200,6 +183,30 @@ func equalCountSubstrings(s string, count int) (ans int) { } ``` +```ts +function equalCountSubstrings(s: string, count: number): number { + const n = s.length; + let ans = 0; + for (let i = 1; i < 27 && i * count <= n; ++i) { + const k = i * count; + const cnt: number[] = Array(26).fill(0); + let t = 0; + for (let j = 0; j < n; ++j) { + const a = s.charCodeAt(j) - 97; + t += ++cnt[a] === count ? 1 : 0; + t -= cnt[a] === count + 1 ? 1 : 0; + if (j >= k) { + const b = s.charCodeAt(j - k) - 97; + t += --cnt[b] === count ? 1 : 0; + t -= cnt[b] === count - 1 ? 1 : 0; + } + ans += i === t ? 1 : 0; + } + } + return ans; +} +``` + ```js /** * @param {string} s @@ -207,25 +214,22 @@ func equalCountSubstrings(s string, count int) (ans int) { * @return {number} */ var equalCountSubstrings = function (s, count) { - let ans = 0; const n = s.length; - for (let x = 1; x <= 26 && x * count <= n; ++x) { - const m = x * count; - const cnt = new Array(26).fill(0); - let y = 0; - for (let i = 0; i < n; ++i) { - const a = s.charCodeAt(i) - 'a'.charCodeAt(0); - ++cnt[a]; - y += cnt[a] == count; - y -= cnt[a] == count + 1; - const j = i - m; - if (j >= 0) { - const b = s.charCodeAt(j) - 'a'.charCodeAt(0); - --cnt[b]; - y += cnt[b] == count; - y -= cnt[b] == count - 1; + let ans = 0; + for (let i = 1; i < 27 && i * count <= n; ++i) { + const k = i * count; + const cnt = Array(26).fill(0); + let t = 0; + for (let j = 0; j < n; ++j) { + const a = s.charCodeAt(j) - 97; + t += ++cnt[a] === count ? 1 : 0; + t -= cnt[a] === count + 1 ? 1 : 0; + if (j >= k) { + const b = s.charCodeAt(j - k) - 97; + t += --cnt[b] === count ? 1 : 0; + t -= cnt[b] === count - 1 ? 1 : 0; } - ans += x == y; + ans += i === t ? 1 : 0; } } return ans; diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/README_EN.md b/solution/2000-2099/2067.Number of Equal Count Substrings/README_EN.md index 11a4fa16a53dd..27a2728ec3b74 100644 --- a/solution/2000-2099/2067.Number of Equal Count Substrings/README_EN.md +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/README_EN.md @@ -57,7 +57,13 @@ Therefore, no substrings in s are equal count substrings, so return 0 ## Solutions -### Solution 1 +### Solution 1: Enumeration + Sliding Window + +We can enumerate the number of types of letters in the substring $x$ within the range of $[1..26]$, then the length of the substring is $count \times x$. + +Next, we take the current substring length as the size of the window, count the number of types of letters in the window size that are equal to $count$, and record it in $y$. If $x = y$ at this time, it means that the number of letters in the current window are all $count$, then we can increment the answer by one. + +The time complexity is $O(n \times C)$, and the space complexity is $O(C)$. Where $n$ is the length of the string $s$, and $C$ is the number of types of letters, in this problem $C = 26$. @@ -65,22 +71,21 @@ Therefore, no substrings in s are equal count substrings, so return 0 class Solution: def equalCountSubstrings(self, s: str, count: int) -> int: ans = 0 - for x in range(1, 27): - m = count * x - if m > len(s): + for i in range(1, 27): + k = i * count + if k > len(s): break cnt = Counter() - y = 0 - for i, c in enumerate(s): + t = 0 + for j, c in enumerate(s): cnt[c] += 1 - y += cnt[c] == count - y -= cnt[c] == count + 1 - j = i - m - if j >= 0: - cnt[s[j]] -= 1 - y += cnt[s[j]] == count - y -= cnt[s[j]] == count - 1 - ans += x == y + t += cnt[c] == count + t -= cnt[c] == count + 1 + if j >= k: + cnt[s[j - k]] -= 1 + t += cnt[s[j - k]] == count + t -= cnt[s[j - k]] == count - 1 + ans += i == t return ans ``` @@ -88,34 +93,24 @@ class Solution: class Solution { public int equalCountSubstrings(String s, int count) { int ans = 0; + int[] cnt = new int[26]; int n = s.length(); - for (int x = 1; x < 27 && count * x <= n; ++x) { - int m = count * x; - int[] cnt = new int[26]; - int y = 0; - for (int i = 0; i < n; ++i) { - int a = s.charAt(i) - 'a'; + for (int i = 1; i < 27 && i * count <= n; ++i) { + int k = i * count; + Arrays.fill(cnt, 0); + int t = 0; + for (int j = 0; j < n; ++j) { + int a = s.charAt(j) - 'a'; ++cnt[a]; - if (cnt[a] == count) { - ++y; - } - if (cnt[a] == count + 1) { - --y; - } - int j = i - m; - if (j >= 0) { - int b = s.charAt(j) - 'a'; + t += cnt[a] == count ? 1 : 0; + t -= cnt[a] == count + 1 ? 1 : 0; + if (j - k >= 0) { + int b = s.charAt(j - k) - 'a'; --cnt[b]; - if (cnt[b] == count) { - ++y; - } - if (cnt[b] == count - 1) { - --y; - } - } - if (x == y) { - ++ans; + t += cnt[b] == count ? 1 : 0; + t -= cnt[b] == count - 1 ? 1 : 0; } + ans += i == t ? 1 : 0; } } return ans; @@ -130,23 +125,20 @@ public: int ans = 0; int n = s.size(); int cnt[26]; - for (int x = 1; x < 27 && count * x <= n; ++x) { - int m = count * x; - memset(cnt, 0, sizeof cnt); - int y = 0; - for (int i = 0; i < n; ++i) { - int a = s[i] - 'a'; - ++cnt[a]; - y += cnt[a] == count; - y -= cnt[a] == count + 1; - int j = i - m; - if (j >= 0) { - int b = s[j] - 'a'; - --cnt[b]; - y += cnt[b] == count; - y -= cnt[b] == count - 1; + for (int i = 1; i < 27 && i * count <= n; ++i) { + int k = i * count; + memset(cnt, 0, sizeof(cnt)); + int t = 0; + for (int j = 0; j < n; ++j) { + int a = s[j] - 'a'; + t += ++cnt[a] == count; + t -= cnt[a] == count + 1; + if (j >= k) { + int b = s[j - k] - 'a'; + t += --cnt[b] == count; + t -= cnt[b] == count - 1; } - ans += x == y; + ans += i == t; } } return ans; @@ -157,31 +149,28 @@ public: ```go func equalCountSubstrings(s string, count int) (ans int) { n := len(s) - for x := 1; x < 27 && x*count <= n; x++ { - m := x * count - y := 0 + for i := 1; i < 27 && i*count <= n; i++ { + k := i * count cnt := [26]int{} - for i, c := range s { + t := 0 + for j, c := range s { a := c - 'a' cnt[a]++ if cnt[a] == count { - y++ - } - if cnt[a] == count+1 { - y-- + t++ + } else if cnt[a] == count+1 { + t-- } - j := i - m - if j >= 0 { - b := s[j] - 'a' + if j >= k { + b := s[j-k] - 'a' cnt[b]-- if cnt[b] == count { - y++ - } - if cnt[b] == count-1 { - y-- + t++ + } else if cnt[b] == count-1 { + t-- } } - if x == y { + if i == t { ans++ } } @@ -190,6 +179,30 @@ func equalCountSubstrings(s string, count int) (ans int) { } ``` +```ts +function equalCountSubstrings(s: string, count: number): number { + const n = s.length; + let ans = 0; + for (let i = 1; i < 27 && i * count <= n; ++i) { + const k = i * count; + const cnt: number[] = Array(26).fill(0); + let t = 0; + for (let j = 0; j < n; ++j) { + const a = s.charCodeAt(j) - 97; + t += ++cnt[a] === count ? 1 : 0; + t -= cnt[a] === count + 1 ? 1 : 0; + if (j >= k) { + const b = s.charCodeAt(j - k) - 97; + t += --cnt[b] === count ? 1 : 0; + t -= cnt[b] === count - 1 ? 1 : 0; + } + ans += i === t ? 1 : 0; + } + } + return ans; +} +``` + ```js /** * @param {string} s @@ -197,25 +210,22 @@ func equalCountSubstrings(s string, count int) (ans int) { * @return {number} */ var equalCountSubstrings = function (s, count) { - let ans = 0; const n = s.length; - for (let x = 1; x <= 26 && x * count <= n; ++x) { - const m = x * count; - const cnt = new Array(26).fill(0); - let y = 0; - for (let i = 0; i < n; ++i) { - const a = s.charCodeAt(i) - 'a'.charCodeAt(0); - ++cnt[a]; - y += cnt[a] == count; - y -= cnt[a] == count + 1; - const j = i - m; - if (j >= 0) { - const b = s.charCodeAt(j) - 'a'.charCodeAt(0); - --cnt[b]; - y += cnt[b] == count; - y -= cnt[b] == count - 1; + let ans = 0; + for (let i = 1; i < 27 && i * count <= n; ++i) { + const k = i * count; + const cnt = Array(26).fill(0); + let t = 0; + for (let j = 0; j < n; ++j) { + const a = s.charCodeAt(j) - 97; + t += ++cnt[a] === count ? 1 : 0; + t -= cnt[a] === count + 1 ? 1 : 0; + if (j >= k) { + const b = s.charCodeAt(j - k) - 97; + t += --cnt[b] === count ? 1 : 0; + t -= cnt[b] === count - 1 ? 1 : 0; } - ans += x == y; + ans += i === t ? 1 : 0; } } return ans; diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.cpp b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.cpp index e022a9b2803c5..13ab78d9e2fc0 100644 --- a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.cpp +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.cpp @@ -4,23 +4,20 @@ class Solution { int ans = 0; int n = s.size(); int cnt[26]; - for (int x = 1; x < 27 && count * x <= n; ++x) { - int m = count * x; - memset(cnt, 0, sizeof cnt); - int y = 0; - for (int i = 0; i < n; ++i) { - int a = s[i] - 'a'; - ++cnt[a]; - y += cnt[a] == count; - y -= cnt[a] == count + 1; - int j = i - m; - if (j >= 0) { - int b = s[j] - 'a'; - --cnt[b]; - y += cnt[b] == count; - y -= cnt[b] == count - 1; + for (int i = 1; i < 27 && i * count <= n; ++i) { + int k = i * count; + memset(cnt, 0, sizeof(cnt)); + int t = 0; + for (int j = 0; j < n; ++j) { + int a = s[j] - 'a'; + t += ++cnt[a] == count; + t -= cnt[a] == count + 1; + if (j >= k) { + int b = s[j - k] - 'a'; + t += --cnt[b] == count; + t -= cnt[b] == count - 1; } - ans += x == y; + ans += i == t; } } return ans; diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.go b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.go index 192805aeef3a9..33425c3236cd5 100644 --- a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.go +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.go @@ -1,30 +1,27 @@ func equalCountSubstrings(s string, count int) (ans int) { n := len(s) - for x := 1; x < 27 && x*count <= n; x++ { - m := x * count - y := 0 + for i := 1; i < 27 && i*count <= n; i++ { + k := i * count cnt := [26]int{} - for i, c := range s { + t := 0 + for j, c := range s { a := c - 'a' cnt[a]++ if cnt[a] == count { - y++ + t++ + } else if cnt[a] == count+1 { + t-- } - if cnt[a] == count+1 { - y-- - } - j := i - m - if j >= 0 { - b := s[j] - 'a' + if j >= k { + b := s[j-k] - 'a' cnt[b]-- if cnt[b] == count { - y++ - } - if cnt[b] == count-1 { - y-- + t++ + } else if cnt[b] == count-1 { + t-- } } - if x == y { + if i == t { ans++ } } diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.java b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.java index 32774b6c9f522..974c374095007 100644 --- a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.java +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.java @@ -1,34 +1,24 @@ class Solution { public int equalCountSubstrings(String s, int count) { int ans = 0; + int[] cnt = new int[26]; int n = s.length(); - for (int x = 1; x < 27 && count * x <= n; ++x) { - int m = count * x; - int[] cnt = new int[26]; - int y = 0; - for (int i = 0; i < n; ++i) { - int a = s.charAt(i) - 'a'; + for (int i = 1; i < 27 && i * count <= n; ++i) { + int k = i * count; + Arrays.fill(cnt, 0); + int t = 0; + for (int j = 0; j < n; ++j) { + int a = s.charAt(j) - 'a'; ++cnt[a]; - if (cnt[a] == count) { - ++y; - } - if (cnt[a] == count + 1) { - --y; - } - int j = i - m; - if (j >= 0) { - int b = s.charAt(j) - 'a'; + t += cnt[a] == count ? 1 : 0; + t -= cnt[a] == count + 1 ? 1 : 0; + if (j - k >= 0) { + int b = s.charAt(j - k) - 'a'; --cnt[b]; - if (cnt[b] == count) { - ++y; - } - if (cnt[b] == count - 1) { - --y; - } - } - if (x == y) { - ++ans; + t += cnt[b] == count ? 1 : 0; + t -= cnt[b] == count - 1 ? 1 : 0; } + ans += i == t ? 1 : 0; } } return ans; diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.js b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.js index b46c628e096b3..793eda918ea38 100644 --- a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.js +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.js @@ -4,25 +4,22 @@ * @return {number} */ var equalCountSubstrings = function (s, count) { - let ans = 0; const n = s.length; - for (let x = 1; x <= 26 && x * count <= n; ++x) { - const m = x * count; - const cnt = new Array(26).fill(0); - let y = 0; - for (let i = 0; i < n; ++i) { - const a = s.charCodeAt(i) - 'a'.charCodeAt(0); - ++cnt[a]; - y += cnt[a] == count; - y -= cnt[a] == count + 1; - const j = i - m; - if (j >= 0) { - const b = s.charCodeAt(j) - 'a'.charCodeAt(0); - --cnt[b]; - y += cnt[b] == count; - y -= cnt[b] == count - 1; + let ans = 0; + for (let i = 1; i < 27 && i * count <= n; ++i) { + const k = i * count; + const cnt = Array(26).fill(0); + let t = 0; + for (let j = 0; j < n; ++j) { + const a = s.charCodeAt(j) - 97; + t += ++cnt[a] === count ? 1 : 0; + t -= cnt[a] === count + 1 ? 1 : 0; + if (j >= k) { + const b = s.charCodeAt(j - k) - 97; + t += --cnt[b] === count ? 1 : 0; + t -= cnt[b] === count - 1 ? 1 : 0; } - ans += x == y; + ans += i === t ? 1 : 0; } } return ans; diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.py b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.py index e8c16d55e3598..1dd97e6a38e0a 100644 --- a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.py +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.py @@ -1,20 +1,19 @@ class Solution: def equalCountSubstrings(self, s: str, count: int) -> int: ans = 0 - for x in range(1, 27): - m = count * x - if m > len(s): + for i in range(1, 27): + k = i * count + if k > len(s): break cnt = Counter() - y = 0 - for i, c in enumerate(s): + t = 0 + for j, c in enumerate(s): cnt[c] += 1 - y += cnt[c] == count - y -= cnt[c] == count + 1 - j = i - m - if j >= 0: - cnt[s[j]] -= 1 - y += cnt[s[j]] == count - y -= cnt[s[j]] == count - 1 - ans += x == y + t += cnt[c] == count + t -= cnt[c] == count + 1 + if j >= k: + cnt[s[j - k]] -= 1 + t += cnt[s[j - k]] == count + t -= cnt[s[j - k]] == count - 1 + ans += i == t return ans diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.ts b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.ts new file mode 100644 index 0000000000000..8fe79e69fce7f --- /dev/null +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.ts @@ -0,0 +1,21 @@ +function equalCountSubstrings(s: string, count: number): number { + const n = s.length; + let ans = 0; + for (let i = 1; i < 27 && i * count <= n; ++i) { + const k = i * count; + const cnt: number[] = Array(26).fill(0); + let t = 0; + for (let j = 0; j < n; ++j) { + const a = s.charCodeAt(j) - 97; + t += ++cnt[a] === count ? 1 : 0; + t -= cnt[a] === count + 1 ? 1 : 0; + if (j >= k) { + const b = s.charCodeAt(j - k) - 97; + t += --cnt[b] === count ? 1 : 0; + t -= cnt[b] === count - 1 ? 1 : 0; + } + ans += i === t ? 1 : 0; + } + } + return ans; +} From f84a1e64e74a2273b2eed764251bad1c83353416 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 11 Apr 2024 18:37:34 +0800 Subject: [PATCH 2/2] fix: solution --- .../2000-2099/2067.Number of Equal Count Substrings/README.md | 4 ++-- .../2067.Number of Equal Count Substrings/README_EN.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/README.md b/solution/2000-2099/2067.Number of Equal Count Substrings/README.md index 1edd1c5999514..8504fbb886139 100644 --- a/solution/2000-2099/2067.Number of Equal Count Substrings/README.md +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/README.md @@ -63,9 +63,9 @@ ### 方法一:枚举 + 滑动窗口 -我们可以在 $[1..26]$ 范围内枚举子串的字母种类数 $x$,那么子串长度就是 $count \times x$。 +我们可以在 $[1..26]$ 范围内枚举子串的字母种类数 $i$,那么子串长度就是 $i \times count$。 -接下来,我们将当前子串长度作为窗口的大小,统计窗口大小中有多少种字母的个数为 $count$,记录在 $y$ 中。如果此时 $x = y$,说明当前窗口中的字母个数都为 $count$,那么就可以将答案加一。 +接下来,我们将当前子串长度作为窗口的大小,统计窗口大小中有多少种字母的个数为 $count$,记录在 $t$ 中。如果此时 $i = t$,说明当前窗口中的字母个数都为 $count$,那么就可以将答案加一。 时间复杂度 $O(n \times C)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度,而 $C$ 为字母的种类数,本题中 $C = 26$。 diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/README_EN.md b/solution/2000-2099/2067.Number of Equal Count Substrings/README_EN.md index 27a2728ec3b74..6f2f598610518 100644 --- a/solution/2000-2099/2067.Number of Equal Count Substrings/README_EN.md +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/README_EN.md @@ -59,9 +59,9 @@ Therefore, no substrings in s are equal count substrings, so return 0 ### Solution 1: Enumeration + Sliding Window -We can enumerate the number of types of letters in the substring $x$ within the range of $[1..26]$, then the length of the substring is $count \times x$. +We can enumerate the number of types of letters in the substring within the range of $[1..26]$, then the length of the substring is $i \times count$. -Next, we take the current substring length as the size of the window, count the number of types of letters in the window size that are equal to $count$, and record it in $y$. If $x = y$ at this time, it means that the number of letters in the current window are all $count$, then we can increment the answer by one. +Next, we take the current substring length as the size of the window, count the number of types of letters in the window size that are equal to $count$, and record it in $t$. If $i = t$ at this time, it means that the number of letters in the current window are all $count$, then we can increment the answer by one. The time complexity is $O(n \times C)$, and the space complexity is $O(C)$. Where $n$ is the length of the string $s$, and $C$ is the number of types of letters, in this problem $C = 26$.