Skip to content

Commit eb4afa4

Browse files
committed
feat: add solutions to lc problem: No.2168
No.2168.Unique Substrings With Equal Digit Frequency
1 parent 9e17960 commit eb4afa4

File tree

8 files changed

+285
-6
lines changed

8 files changed

+285
-6
lines changed

solution/0800-0899/0885.Spiral Matrix III/Solution.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def spiralMatrixIII(self, rows: int, cols: int, rStart: int, cStart: int) -> List[List[int]]:
2+
def spiralMatrixIII(
3+
self, rows: int, cols: int, rStart: int, cStart: int
4+
) -> List[List[int]]:
35
ans = [[rStart, cStart]]
46
if rows * cols == 1:
57
return ans

solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def countSquares(self, matrix: List[List[int]]) -> int:
1010
if i == 0 or j == 0:
1111
f[i][j] = 1
1212
else:
13-
f[i][j] = min(f[i - 1][j - 1], f[i - 1]
14-
[j], f[i][j - 1]) + 1
13+
f[i][j] = min(f[i - 1][j - 1], f[i - 1][j], f[i][j - 1]) + 1
1514
ans += f[i][j]
1615
return ans

solution/1700-1799/1712.Ways to Split Array Into Three Subarrays/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ def waysToSplit(self, nums: List[int]) -> int:
2525
else:
2626
left = mid + 1
2727
ans += (mid_right - left + 1) % mod
28-
return int(ans % mod)
28+
return int(ans % mod)

solution/2100-2199/2168.Unique Substrings With Equal Digit Frequency/README.md

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,109 @@
4747
<!-- 这里可写当前语言的特殊实现逻辑 -->
4848

4949
```python
50-
50+
class Solution:
51+
def equalDigitFrequency(self, s: str) -> int:
52+
def check(i, j):
53+
v = set()
54+
for k in range(10):
55+
cnt = presum[j + 1][k] - presum[i][k]
56+
if cnt > 0:
57+
v.add(cnt)
58+
if len(v) > 1:
59+
return False
60+
return True
61+
62+
n = len(s)
63+
presum = [[0] * 10 for _ in range(n + 1)]
64+
for i, c in enumerate(s):
65+
presum[i + 1][int(c)] += 1
66+
for j in range(10):
67+
presum[i + 1][j] += presum[i][j]
68+
vis = set(s[i: j + 1] for i in range(n)
69+
for j in range(i, n) if check(i, j))
70+
return len(vis)
5171
```
5272

5373
### **Java**
5474

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

5777
```java
78+
class Solution {
79+
public int equalDigitFrequency(String s) {
80+
int n = s.length();
81+
int[][] presum = new int[n + 1][10];
82+
for (int i = 0; i < n; ++i) {
83+
++presum[i + 1][s.charAt(i) - '0'];
84+
for (int j = 0; j < 10; ++j) {
85+
presum[i + 1][j] += presum[i][j];
86+
}
87+
}
88+
Set<String> vis = new HashSet<>();
89+
for (int i = 0; i < n; ++i) {
90+
for (int j = i; j < n; ++j) {
91+
if (check(i, j, presum)) {
92+
vis.add(s.substring(i, j + 1));
93+
}
94+
}
95+
}
96+
return vis.size();
97+
}
98+
99+
private boolean check(int i, int j, int[][] presum) {
100+
Set<Integer> v = new HashSet<>();
101+
for (int k = 0; k < 10; ++k) {
102+
int cnt = presum[j + 1][k] - presum[i][k];
103+
if (cnt > 0) {
104+
v.add(cnt);
105+
}
106+
if (v.size() > 1) {
107+
return false;
108+
}
109+
}
110+
return true;
111+
}
112+
}
113+
```
58114

115+
### **Go**
116+
117+
```go
118+
func equalDigitFrequency(s string) int {
119+
n := len(s)
120+
presum := make([][]int, n+1)
121+
for i := range presum {
122+
presum[i] = make([]int, 10)
123+
}
124+
for i, c := range s {
125+
presum[i+1][c-'0']++
126+
for j := 0; j < 10; j++ {
127+
presum[i+1][j] += presum[i][j]
128+
}
129+
}
130+
check := func(i, j int) bool {
131+
v := make(map[int]bool)
132+
for k := 0; k < 10; k++ {
133+
cnt := presum[j+1][k] - presum[i][k]
134+
if cnt > 0 {
135+
v[cnt] = true
136+
}
137+
if len(v) > 1 {
138+
return false
139+
}
140+
}
141+
return true
142+
}
143+
vis := make(map[string]bool)
144+
for i := 0; i < n; i++ {
145+
for j := i; j < n; j++ {
146+
if check(i, j) {
147+
vis[s[i:j+1]] = true
148+
}
149+
}
150+
}
151+
return len(vis)
152+
}
59153
```
60154

61155
### **TypeScript**

solution/2100-2199/2168.Unique Substrings With Equal Digit Frequency/README_EN.md

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,107 @@ Note that although the substring &quot;12&quot; appears twice, it is only counte
3939
### **Python3**
4040

4141
```python
42-
42+
class Solution:
43+
def equalDigitFrequency(self, s: str) -> int:
44+
def check(i, j):
45+
v = set()
46+
for k in range(10):
47+
cnt = presum[j + 1][k] - presum[i][k]
48+
if cnt > 0:
49+
v.add(cnt)
50+
if len(v) > 1:
51+
return False
52+
return True
53+
54+
n = len(s)
55+
presum = [[0] * 10 for _ in range(n + 1)]
56+
for i, c in enumerate(s):
57+
presum[i + 1][int(c)] += 1
58+
for j in range(10):
59+
presum[i + 1][j] += presum[i][j]
60+
vis = set(s[i: j + 1] for i in range(n)
61+
for j in range(i, n) if check(i, j))
62+
return len(vis)
4363
```
4464

4565
### **Java**
4666

4767
```java
68+
class Solution {
69+
public int equalDigitFrequency(String s) {
70+
int n = s.length();
71+
int[][] presum = new int[n + 1][10];
72+
for (int i = 0; i < n; ++i) {
73+
++presum[i + 1][s.charAt(i) - '0'];
74+
for (int j = 0; j < 10; ++j) {
75+
presum[i + 1][j] += presum[i][j];
76+
}
77+
}
78+
Set<String> vis = new HashSet<>();
79+
for (int i = 0; i < n; ++i) {
80+
for (int j = i; j < n; ++j) {
81+
if (check(i, j, presum)) {
82+
vis.add(s.substring(i, j + 1));
83+
}
84+
}
85+
}
86+
return vis.size();
87+
}
88+
89+
private boolean check(int i, int j, int[][] presum) {
90+
Set<Integer> v = new HashSet<>();
91+
for (int k = 0; k < 10; ++k) {
92+
int cnt = presum[j + 1][k] - presum[i][k];
93+
if (cnt > 0) {
94+
v.add(cnt);
95+
}
96+
if (v.size() > 1) {
97+
return false;
98+
}
99+
}
100+
return true;
101+
}
102+
}
103+
```
48104

105+
### **Go**
106+
107+
```go
108+
func equalDigitFrequency(s string) int {
109+
n := len(s)
110+
presum := make([][]int, n+1)
111+
for i := range presum {
112+
presum[i] = make([]int, 10)
113+
}
114+
for i, c := range s {
115+
presum[i+1][c-'0']++
116+
for j := 0; j < 10; j++ {
117+
presum[i+1][j] += presum[i][j]
118+
}
119+
}
120+
check := func(i, j int) bool {
121+
v := make(map[int]bool)
122+
for k := 0; k < 10; k++ {
123+
cnt := presum[j+1][k] - presum[i][k]
124+
if cnt > 0 {
125+
v[cnt] = true
126+
}
127+
if len(v) > 1 {
128+
return false
129+
}
130+
}
131+
return true
132+
}
133+
vis := make(map[string]bool)
134+
for i := 0; i < n; i++ {
135+
for j := i; j < n; j++ {
136+
if check(i, j) {
137+
vis[s[i:j+1]] = true
138+
}
139+
}
140+
}
141+
return len(vis)
142+
}
49143
```
50144

51145
### **TypeScript**
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
func equalDigitFrequency(s string) int {
2+
n := len(s)
3+
presum := make([][]int, n+1)
4+
for i := range presum {
5+
presum[i] = make([]int, 10)
6+
}
7+
for i, c := range s {
8+
presum[i+1][c-'0']++
9+
for j := 0; j < 10; j++ {
10+
presum[i+1][j] += presum[i][j]
11+
}
12+
}
13+
check := func(i, j int) bool {
14+
v := make(map[int]bool)
15+
for k := 0; k < 10; k++ {
16+
cnt := presum[j+1][k] - presum[i][k]
17+
if cnt > 0 {
18+
v[cnt] = true
19+
}
20+
if len(v) > 1 {
21+
return false
22+
}
23+
}
24+
return true
25+
}
26+
vis := make(map[string]bool)
27+
for i := 0; i < n; i++ {
28+
for j := i; j < n; j++ {
29+
if check(i, j) {
30+
vis[s[i:j+1]] = true
31+
}
32+
}
33+
}
34+
return len(vis)
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int equalDigitFrequency(String s) {
3+
int n = s.length();
4+
int[][] presum = new int[n + 1][10];
5+
for (int i = 0; i < n; ++i) {
6+
++presum[i + 1][s.charAt(i) - '0'];
7+
for (int j = 0; j < 10; ++j) {
8+
presum[i + 1][j] += presum[i][j];
9+
}
10+
}
11+
Set<String> vis = new HashSet<>();
12+
for (int i = 0; i < n; ++i) {
13+
for (int j = i; j < n; ++j) {
14+
if (check(i, j, presum)) {
15+
vis.add(s.substring(i, j + 1));
16+
}
17+
}
18+
}
19+
return vis.size();
20+
}
21+
22+
private boolean check(int i, int j, int[][] presum) {
23+
Set<Integer> v = new HashSet<>();
24+
for (int k = 0; k < 10; ++k) {
25+
int cnt = presum[j + 1][k] - presum[i][k];
26+
if (cnt > 0) {
27+
v.add(cnt);
28+
}
29+
if (v.size() > 1) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
}
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def equalDigitFrequency(self, s: str) -> int:
3+
def check(i, j):
4+
v = set()
5+
for k in range(10):
6+
cnt = presum[j + 1][k] - presum[i][k]
7+
if cnt > 0:
8+
v.add(cnt)
9+
if len(v) > 1:
10+
return False
11+
return True
12+
13+
n = len(s)
14+
presum = [[0] * 10 for _ in range(n + 1)]
15+
for i, c in enumerate(s):
16+
presum[i + 1][int(c)] += 1
17+
for j in range(10):
18+
presum[i + 1][j] += presum[i][j]
19+
vis = set(s[i : j + 1] for i in range(n) for j in range(i, n) if check(i, j))
20+
return len(vis)

0 commit comments

Comments
 (0)