Skip to content

Commit 5ccd897

Browse files
committed
feat: add solutions to lc problem: No.1876. Substrings of Size Three with Distinct Characters
1 parent 886984f commit 5ccd897

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

solution/1800-1899/1876.Substrings of Size Three with Distinct Characters/README.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
<li><code>s</code>​​​​​​ 只包含小写英文字母。</li>
4444
</ul>
4545

46-
4746
## 解法
4847

4948
<!-- 这里可写通用的实现逻辑 -->
@@ -55,15 +54,31 @@
5554
<!-- 这里可写当前语言的特殊实现逻辑 -->
5655

5756
```python
58-
57+
class Solution:
58+
def countGoodSubstrings(self, s: str) -> int:
59+
count, n = 0, len(s)
60+
for i in range(n - 2):
61+
count += (s[i] != s[i + 1] and s[i] != s[i + 2] and s[i + 1] != s[i + 2])
62+
return count
5963
```
6064

6165
### **Java**
6266

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

6569
```java
66-
70+
class Solution {
71+
public int countGoodSubstrings(String s) {
72+
int count = 0, n = s.length();
73+
for (int i = 0; i < n - 2; ++i) {
74+
char a = s.charAt(i), b = s.charAt(i + 1), c = s.charAt(i + 2);
75+
if (a != b && a != c && b != c) {
76+
++count;
77+
}
78+
}
79+
return count;
80+
}
81+
}
6782
```
6883

6984
### **...**

solution/1800-1899/1876.Substrings of Size Three with Distinct Characters/README_EN.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,36 @@ The good substrings are &quot;abc&quot;, &quot;bca&quot;, &quot;cab&quot;, and &
3939
<li><code>s</code>​​​​​​ consists of lowercase English letters.</li>
4040
</ul>
4141

42-
4342
## Solutions
4443

4544
<!-- tabs:start -->
4645

4746
### **Python3**
4847

4948
```python
50-
49+
class Solution:
50+
def countGoodSubstrings(self, s: str) -> int:
51+
count, n = 0, len(s)
52+
for i in range(n - 2):
53+
count += (s[i] != s[i + 1] and s[i] != s[i + 2] and s[i + 1] != s[i + 2])
54+
return count
5155
```
5256

5357
### **Java**
5458

5559
```java
56-
60+
class Solution {
61+
public int countGoodSubstrings(String s) {
62+
int count = 0, n = s.length();
63+
for (int i = 0; i < n - 2; ++i) {
64+
char a = s.charAt(i), b = s.charAt(i + 1), c = s.charAt(i + 2);
65+
if (a != b && a != c && b != c) {
66+
++count;
67+
}
68+
}
69+
return count;
70+
}
71+
}
5772
```
5873

5974
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int countGoodSubstrings(String s) {
3+
int count = 0, n = s.length();
4+
for (int i = 0; i < n - 2; ++i) {
5+
char a = s.charAt(i), b = s.charAt(i + 1), c = s.charAt(i + 2);
6+
if (a != b && a != c && b != c) {
7+
++count;
8+
}
9+
}
10+
return count;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def countGoodSubstrings(self, s: str) -> int:
3+
count, n = 0, len(s)
4+
for i in range(n - 2):
5+
count += (s[i] != s[i + 1] and s[i] != s[i + 2] and s[i + 1] != s[i + 2])
6+
return count

0 commit comments

Comments
 (0)