A string is good if there are no repeated characters.
Given a string s
, return the number of good substrings of length three in s
.
Note that if there are multiple occurrences of the same substring, every occurrence should be counted.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: s = "xyzzaz" Output: 1 Explanation: There are 4 substrings of size 3: "xyz", "yzz", "zza", and "zaz". The only good substring of length 3 is "xyz".
Example 2:
Input: s = "aababcabc" Output: 4 Explanation: There are 7 substrings of size 3: "aab", "aba", "bab", "abc", "bca", "cab", and "abc". The good substrings are "abc", "bca", "cab", and "abc".
Constraints:
1 <= s.length <= 100
s
consists of lowercase English letters.This problem asks us to find the number of substrings of length 3 in a given string s
that contain only distinct characters (no repeated characters). We can solve this efficiently using a sliding window approach.
Approach:
Sliding Window: We'll use a sliding window of size 3 to traverse the string. The window will move one character at a time.
Distinct Character Check: For each window, we need to check if it contains only distinct characters. We can achieve this efficiently using a bitmask.
Bitmask: A bitmask is a binary integer where each bit represents a character (a-z). If a character is present in the window, the corresponding bit is set to 1. If the bit is already set (meaning the character is repeated), we know the window is not "good".
Counting Good Substrings: If a window contains only distinct characters (no bits are set more than once), we increment our count of "good" substrings.
Time Complexity Analysis:
The sliding window iterates through the string once. The operations within the loop (bit manipulation and checking window size) take constant time, O(1). Therefore, the overall time complexity is O(n), where n is the length of the string.
Space Complexity Analysis:
The space used is primarily for the bitmask and a few integer variables. This space remains constant regardless of the input string size. Thus, the space complexity is O(1).
Code Implementation (Python):
class Solution:
def countGoodSubstrings(self, s: str) -> int:
count = 0 # Initialize count of good substrings
mask = 0 # Bitmask to track characters in the window
left = 0 # Left pointer of the window
for right, char in enumerate(s):
index = ord(char) - ord('a') # Get the index (0-25) for the character
# Check if the character is already in the window (bit is set)
while mask >> index & 1:
mask ^= 1 << (ord(s[left]) - ord('a')) # Remove leftmost character from mask
left += 1 # Move left pointer
mask |= 1 << index # Add current character to the mask
# Check if the window has size 3 and contains distinct characters
if right - left + 1 == 3:
count += 1
return count
Code Implementation in other languages (brief overview):
The logic remains the same across different languages. The main differences lie in syntax and data type handling:
int
for the mask.int
for the mask.int
for the mask.number
for the mask.int
for the mask.The core algorithm – sliding window with a bitmask – stays consistent for optimal performance across all languages. The provided code examples showcase efficient implementations of this algorithm.