Unit 4 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Iterate through the string using a fixed-size window of three characters. At each position, check if all characters in the window are unique.
1) Initialize a counter for good substrings to 0.
2) Loop through the string until there are less than three characters left to form a substring.
3) For each position, extract a substring of three characters.
4) Convert the substring into a set to remove duplicates and check its length.
5) If the length of the set is 3, increment the counter for good substrings.
6) Return the total count of good substrings found.
⚠️ Common Mistakes
def count_good_substrings(s):
# Initialize the count of good substrings
count = 0
# Loop through the string, stopping when less than 3 characters remain
for i in range(len(s) - 2):
# Extract the current window of size 3
window = s[i:i+3]
# Check if all characters in the window are unique
if len(window) == len(set(window)):
count += 1
return count