Solving Leetcode Interviews in Seconds with AI: Count Complete Substrings
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2953" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You are given a string word and an integer k. A substring s of word is complete if: Each character in s occurs exactly k times. The difference between two adjacent characters is at most 2. That is, for any two adjacent characters c1 and c2 in s, the absolute difference in their positions in the alphabet is at most 2. Return the number of complete substrings of word. A substring is a non-empty contiguous sequence of characters in a string. Example 1: Input: word = "igigee", k = 2 Output: 3 Explanation: The complete substrings where each character appears exactly twice and the difference between adjacent characters is at most 2 are: igigee, igigee, igigee. Example 2: Input: word = "aaabbbccc", k = 3 Output: 6 Explanation: The complete substrings where each character appears exactly three times and the difference between adjacent characters is at most 2 are: aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc. Constraints: 1 <= word.length <= 105 word consists only of lowercase English letters. 1 <= k <= word.length
Explanation
- Sliding Window Approach: Iterate through all possible substring lengths and starting positions. For each substring, check if it's complete by counting character frequencies and verifying the adjacency condition.
- Frequency Counting: Use a dictionary (or array) to efficiently count the occurrences of each character within the current sliding window.
- Adjacency Check: Iterate through the substring to verify that the absolute difference between adjacent characters is at most 2.
- Time Complexity: O(n*26), where n is the length of the word, and 26 is the maximum substring length to check for. Worst case: O(n). Space Complexity: O(26) due to the frequency map. Worst case: O(1).
Code
def complete_substrings(word: str, k: int) -> int:
"""
Finds the number of complete substrings of a word.
Args:
word: The input string.
k: The required frequency of each character.
Returns:
The number of complete substrings.
"""
n = len(word)
count = 0
for length in range(1, n + 1):
if length > 26 * k:
break # Optimization: A complete substring can't be longer than this
for i in range(n - length + 1):
substring = word[i:i + length]
freq = {}
valid = True
# Count frequencies
for char in substring:
freq[char] = freq.get(char, 0) + 1
# Check if each character appears exactly k times
for char in freq:
if freq[char] != k:
valid = False
break
# Check if the difference between adjacent characters is at most 2
if valid:
for j in range(len(substring) - 1):
if abs(ord(substring[j]) - ord(substring[j + 1])) > 2:
valid = False
break
if valid and len(freq) * k == length: #Optimization - to make sure the length is a multiple of k and the total distinct characters count makes sense.
count += 1
return count