Skip to content

Commit 74382b3

Browse files
committed
Solve issue #1876
1 parent fed9345 commit 74382b3

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def good_substrings(s: str) -> int:
2+
"""
3+
A substring is called good if all of the characters in the substring are different.
4+
5+
Given a string s, return the number of good substrings of length three in s.
6+
7+
Args:
8+
s (str): The input string.
9+
10+
Returns:
11+
int: The number of good substrings of length three in s.
12+
13+
Example:
14+
Input: s = "smithech"
15+
Output: 6
16+
Explanation: The good substrings are "smi", "mit", "ith", "the", "hec", "ech".
17+
18+
"""
19+
count = 0
20+
21+
# len(s) - 2 because the last 3-character substring starts at index len(s)-3
22+
for i in range(len(s) - 2):
23+
# The set() function removes duplicate characters, so if the length of the set is 3, it means all characters are different
24+
if len(set(s[i:i + 3])) == 3:
25+
count += 1
26+
return count
27+
28+
if __name__ == "__main__":
29+
example = "Smithech"
30+
result = good_substrings(example)
31+
print("Input: ", example)
32+
print("Output: ", result)

0 commit comments

Comments
 (0)