Skip to content

Commit 3d27848

Browse files
committed
feat: add solutions to lc problem: No.1358
No.1358.Number of Substrings Containing All Three Characters
1 parent d0efb2a commit 3d27848

File tree

6 files changed

+167
-2
lines changed

6 files changed

+167
-2
lines changed

solution/1300-1399/1358.Number of Substrings Containing All Three Characters/README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,85 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:一次遍历**
49+
50+
我们用一个长度为 $3$ 的数组 $d$ 记录三种字符最近一次出现的位置,初始时均为 $-1$。
51+
52+
遍历字符串 $s$,对于当前位置 $i$,我们先更新 $d[s[i]=i$,然后合法的字符串个数为 $min(d[0], d[1], d[2]) + 1$,累加到答案中。
53+
54+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
55+
4856
<!-- tabs:start -->
4957

5058
### **Python3**
5159

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

5462
```python
55-
63+
class Solution:
64+
def numberOfSubstrings(self, s: str) -> int:
65+
d = {"a": -1, "b": -1, "c": -1}
66+
ans = 0
67+
for i, c in enumerate(s):
68+
d[c] = i
69+
ans += min(d["a"], d["b"], d["c"]) + 1
70+
return ans
5671
```
5772

5873
### **Java**
5974

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

6277
```java
78+
class Solution {
79+
public int numberOfSubstrings(String s) {
80+
int[] d = new int[] {-1, -1, -1};
81+
int ans = 0;
82+
for (int i = 0; i < s.length(); ++i) {
83+
char c = s.charAt(i);
84+
d[c - 'a'] = i;
85+
ans += Math.min(d[0], Math.min(d[1], d[2])) + 1;
86+
}
87+
return ans;
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int numberOfSubstrings(string s) {
98+
int d[3] = {-1, -1, -1};
99+
int ans = 0;
100+
for (int i = 0; i < s.size(); ++i) {
101+
d[s[i] - 'a'] = i;
102+
ans += min(d[0], min(d[1], d[2])) + 1;
103+
}
104+
return ans;
105+
}
106+
};
107+
```
63108
109+
### **Go**
110+
111+
```go
112+
func numberOfSubstrings(s string) (ans int) {
113+
d := [3]int{-1, -1, -1}
114+
for i, c := range s {
115+
d[c-'a'] = i
116+
ans += min(d[0], min(d[1], d[2])) + 1
117+
}
118+
return
119+
}
120+
121+
func min(a, b int) int {
122+
if a < b {
123+
return a
124+
}
125+
return b
126+
}
64127
```
65128

66129
### **...**

solution/1300-1399/1358.Number of Substrings Containing All Three Characters/README_EN.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,68 @@
4747
### **Python3**
4848

4949
```python
50-
50+
class Solution:
51+
def numberOfSubstrings(self, s: str) -> int:
52+
d = {"a": -1, "b": -1, "c": -1}
53+
ans = 0
54+
for i, c in enumerate(s):
55+
d[c] = i
56+
ans += min(d["a"], d["b"], d["c"]) + 1
57+
return ans
5158
```
5259

5360
### **Java**
5461

5562
```java
63+
class Solution {
64+
public int numberOfSubstrings(String s) {
65+
int[] d = new int[] {-1, -1, -1};
66+
int ans = 0;
67+
for (int i = 0; i < s.length(); ++i) {
68+
char c = s.charAt(i);
69+
d[c - 'a'] = i;
70+
ans += Math.min(d[0], Math.min(d[1], d[2])) + 1;
71+
}
72+
return ans;
73+
}
74+
}
75+
```
76+
77+
### **C++**
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
int numberOfSubstrings(string s) {
83+
int d[3] = {-1, -1, -1};
84+
int ans = 0;
85+
for (int i = 0; i < s.size(); ++i) {
86+
d[s[i] - 'a'] = i;
87+
ans += min(d[0], min(d[1], d[2])) + 1;
88+
}
89+
return ans;
90+
}
91+
};
92+
```
5693
94+
### **Go**
95+
96+
```go
97+
func numberOfSubstrings(s string) (ans int) {
98+
d := [3]int{-1, -1, -1}
99+
for i, c := range s {
100+
d[c-'a'] = i
101+
ans += min(d[0], min(d[1], d[2])) + 1
102+
}
103+
return
104+
}
105+
106+
func min(a, b int) int {
107+
if a < b {
108+
return a
109+
}
110+
return b
111+
}
57112
```
58113

59114
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int numberOfSubstrings(string s) {
4+
int d[3] = {-1, -1, -1};
5+
int ans = 0;
6+
for (int i = 0; i < s.size(); ++i) {
7+
d[s[i] - 'a'] = i;
8+
ans += min(d[0], min(d[1], d[2])) + 1;
9+
}
10+
return ans;
11+
}
12+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func numberOfSubstrings(s string) (ans int) {
2+
d := [3]int{-1, -1, -1}
3+
for i, c := range s {
4+
d[c-'a'] = i
5+
ans += min(d[0], min(d[1], d[2])) + 1
6+
}
7+
return
8+
}
9+
10+
func min(a, b int) int {
11+
if a < b {
12+
return a
13+
}
14+
return b
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int numberOfSubstrings(String s) {
3+
int[] d = new int[] {-1, -1, -1};
4+
int ans = 0;
5+
for (int i = 0; i < s.length(); ++i) {
6+
char c = s.charAt(i);
7+
d[c - 'a'] = i;
8+
ans += Math.min(d[0], Math.min(d[1], d[2])) + 1;
9+
}
10+
return ans;
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def numberOfSubstrings(self, s: str) -> int:
3+
d = {"a": -1, "b": -1, "c": -1}
4+
ans = 0
5+
for i, c in enumerate(s):
6+
d[c] = i
7+
ans += min(d["a"], d["b"], d["c"]) + 1
8+
return ans

0 commit comments

Comments
 (0)