Construct String With Repeat Limit - Complete Solution Guide
Construct String With Repeat Limit is LeetCode problem 2182, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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 .
Detailed Explanation
The problem asks us to construct the lexicographically largest string from the given string 's' such that no character repeats more than 'repeatLimit' times consecutively. We don't necessarily have to use all the characters from 's'. The inputs are the string 's' and the integer 'repeatLimit'. The output is the lexicographically largest string satisfying the constraint.
Solution Approach
The solution uses a greedy approach. We start with the largest character ('z') and try to append it to the result as many times as possible, up to the repeat limit. If we exhaust all occurrences of 'z', we move on to the next largest character. If we hit the repeat limit for a character before exhausting all occurrences, we find the next largest available character and append it once to break the repetition. We continue this process until we run out of characters to append.
Step-by-Step Algorithm
- Step 1: Create a frequency count array (counts) of size 26 to store the number of occurrences of each lowercase English letter in the input string 's'.
- Step 2: Initialize two pointers, 'i' and 'j', to 25 and 24 respectively. 'i' represents the current largest character we're considering, and 'j' represents the next largest character to use as a breaker when 'i' hits repeatLimit.
- Step 3: Iterate while 'i' is greater than or equal to 0. In each iteration, check if counts[i] is greater than 0. If not, decrement 'i' and continue to the next iteration.
- Step 4: Calculate the number of times (num_to_add) we can add the current character 'i' to the result. It's the minimum of counts[i] and repeatLimit.
- Step 5: Append the character 'i' num_to_add times to the result string. Update counts[i] by subtracting num_to_add.
- Step 6: If counts[i] is still greater than 0, it means we hit the repeat limit. We need to find a breaker character. Set j = min(j, i - 1) and decrement j until we find a character with a positive count.
- Step 7: If we find a breaker character (j >= 0), append it once to the result and decrement counts[j]. Otherwise (j < 0), it means there are no smaller characters available, so we break the loop as we can't satisfy the repeatLimit constraint for character i.
- Step 8: If counts[i] is 0, it means we exhausted all occurrences of the current character 'i', so decrement 'i' to consider the next smaller character.
- Step 9: Finally, return the result string.
Key Insights
- Insight 1: Since we want the lexicographically largest string, we should start by using the largest character ('z') as much as possible until either we exhaust all 'z's or we reach the repeat limit.
- Insight 2: If we reach the repeat limit for a character, we need to use a smaller character to break the repetition, but we should use the next largest available character to maintain the lexicographical order.
- Insight 3: We can use a frequency count array to keep track of the number of occurrences of each character in the input string. This allows us to efficiently find the available characters.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Hash Table, String, Greedy, Heap (Priority Queue), Counting.
Companies
Asked at: Arista Networks, Fortinet.