Solving Leetcode Interviews in Seconds with AI: Longest Substring of One Repeating Character
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2213" 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 0-indexed string s. You are also given a 0-indexed string queryCharacters of length k and a 0-indexed array of integer indices queryIndices of length k, both of which are used to describe k queries. The ith query updates the character in s at index queryIndices[i] to the character queryCharacters[i]. Return an array lengths of length k where lengths[i] is the length of the longest substring of s consisting of only one repeating character after the ith query is performed. Example 1: Input: s = "babacc", queryCharacters = "bcb", queryIndices = [1,3,3] Output: [3,3,4] Explanation: - 1st query updates s = "bbbacc". The longest substring consisting of one repeating character is "bbb" with length 3. - 2nd query updates s = "bbbccc". The longest substring consisting of one repeating character can be "bbb" or "ccc" with length 3. - 3rd query updates s = "bbbbcc". The longest substring consisting of one repeating character is "bbbb" with length 4. Thus, we return [3,3,4]. Example 2: Input: s = "abyzz", queryCharacters = "aa", queryIndices = [2,1] Output: [2,3] Explanation: - 1st query updates s = "abazz". The longest substring consisting of one repeating character is "zz" with length 2. - 2nd query updates s = "aaazz". The longest substring consisting of one repeating character is "aaa" with length 3. Thus, we return [2,3]. Constraints: 1 <= s.length <= 105 s consists of lowercase English letters. k == queryCharacters.length == queryIndices.length 1 <= k <= 105 queryCharacters consists of lowercase English letters. 0 <= queryIndices[i] < s.length
Explanation
- Iterate through queries: For each query, update the string
sat the given index with the given character.- Calculate longest substring: After each update, iterate through the modified string
sto find the longest substring consisting of the same repeating character. - Store and return lengths: Store the length of the longest substring after each query in an array
lengthsand return it.
- Calculate longest substring: After each update, iterate through the modified string
- Runtime Complexity: O(k*n), where k is the number of queries and n is the length of the string s.
- Storage Complexity: O(k), where k is the number of queries, to store the lengths array.
Code
def longestRepeating(s: str, queryCharacters: str, queryIndices: list[int]) -> list[int]:
"""
Given a 0-indexed string s. You are also given a 0-indexed string queryCharacters of length k and a 0-indexed array of integer indices queryIndices of length k, both of which are used to describe k queries. The ith query updates the character in s at index queryIndices[i] to the character queryCharacters[i].
Return an array lengths of length k where lengths[i] is the length of the longest substring of s consisting of only one repeating character after the ith query is performed.
Example:
----------
s = "babacc", queryCharacters = "bcb", queryIndices = [1,3,3]
Output: [3,3,4]
s = "abyzz", queryCharacters = "aa", queryIndices = [2,1]
Output: [2,3]
"""
lengths = []
s_list = list(s)
for i in range(len(queryCharacters)):
s_list[queryIndices[i]] = queryCharacters[i]
current_s = "".join(s_list)
max_len = 0
current_len = 0
for j in range(len(current_s)):
if j > 0 and current_s[j] == current_s[j - 1]:
current_len += 1
else:
current_len = 1
max_len = max(max_len, current_len)
lengths.append(max_len)
return lengths