Solving Leetcode Interviews in Seconds with AI: Construct String With Repeat Limit
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2182" 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 s and an integer repeatLimit. Construct a new string repeatLimitedString using the characters of s such that no letter appears more than repeatLimit times in a row. You do not have to use all characters from s. Return the lexicographically largest repeatLimitedString possible. A string a is lexicographically larger than a string b if in the first position where a and b differ, string a has a letter that appears later in the alphabet than the corresponding letter in b. If the first min(a.length, b.length) characters do not differ, then the longer string is the lexicographically larger one. Example 1: Input: s = "cczazcc", repeatLimit = 3 Output: "zzcccac" Explanation: We use all of the characters from s to construct the repeatLimitedString "zzcccac". The letter 'a' appears at most 1 time in a row. The letter 'c' appears at most 3 times in a row. The letter 'z' appears at most 2 times in a row. Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString. The string is the lexicographically largest repeatLimitedString possible so we return "zzcccac". Note that the string "zzcccca" is lexicographically larger but the letter 'c' appears more than 3 times in a row, so it is not a valid repeatLimitedString. Example 2: Input: s = "aababab", repeatLimit = 2 Output: "bbabaa" Explanation: We use only some of the characters from s to construct the repeatLimitedString "bbabaa". The letter 'a' appears at most 2 times in a row. The letter 'b' appears at most 2 times in a row. Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString. The string is the lexicographically largest repeatLimitedString possible so we return "bbabaa". Note that the string "bbabaaa" is lexicographically larger but the letter 'a' appears more than 2 times in a row, so it is not a valid repeatLimitedString. Constraints: 1 <= repeatLimit <= s.length <= 105 s consists of lowercase English letters.
Explanation
Here's the solution:
- Count the frequency of each character in the input string.
- Iterate through the characters in descending order (from 'z' to 'a'). For each character, append it to the result string as many times as possible (up to
repeatLimit), while ensuring the limit is not exceeded. If we exhaust the count for a character before usingrepeatLimittimes, move to the next character. If we can't add any more of the current character without exceeding the limit, and we still have more of it to add, look for a smaller character to insert as a break. If no smaller character is available to break the repetition, stop adding characters.
Runtime Complexity: O(N + 26), where N is the length of the input string. Storage Complexity: O(26) = O(1).
Code
def repeatLimitedString(s: str, repeatLimit: int) -> str:
counts = {}
for char in s:
counts[char] = counts.get(char, 0) + 1
result = ""
sorted_chars = sorted(counts.keys(), reverse=True)
i = 0
while i < len(sorted_chars):
char = sorted_chars[i]
if counts[char] == 0:
i += 1
continue
repeat = min(counts[char], repeatLimit)
result += char * repeat
counts[char] -= repeat
if counts[char] > 0:
j = i + 1
while j < len(sorted_chars) and counts[sorted_chars[j]] == 0:
j += 1
if j == len(sorted_chars):
break
next_char = sorted_chars[j]
result += next_char
counts[next_char] -= 1
return result