Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions exercises/1000_programs/medium/1876_good_substrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def good_substrings(s: str) -> int:
"""
A substring is called good if all of the characters in the substring are different.

Given a string s, return the number of good substrings of length three in s.

Args:
s (str): The input string.

Returns:
int: The number of good substrings of length three in s.

Example:
Input: s = "smithech"
Output: 6
Explanation: The good substrings are "smi", "mit", "ith", "the", "hec", "ech".

"""
count = 0

# len(s) - 2 because the last 3-character substring starts at index len(s)-3
for i in range(len(s) - 2):
# The set() function removes duplicate characters, so if the length of the set is 3, it means all characters are different
if len(set(s[i:i + 3])) == 3:
count += 1
return count

if __name__ == "__main__":
example = "Smithech"
result = good_substrings(example)
print("Input: ", example)
print("Output: ", result)