Similar Problems
Similar Problems not available
Construct String With Repeat Limit - Leetcode Solution
Companies:
LeetCode: Construct String With Repeat Limit Leetcode Solution
Difficulty: Medium
Topics: greedy string heap-priority-queue
Problem Statement:
Given a string s and an integer k, return a string that satisfies the following conditions:
- The resulting string is exactly s, with no additional characters.
- If there are multiple ways to satisfy the above conditions, choose the one that minimizes the length of the resulting string.
- If it is not possible to satisfy the above conditions, return an empty string.
To satisfy the above conditions, we need to consider the following cases:
-
k is greater than or equal to the length of s: In this case, we can simply repeat the string s k times to get the resulting string. For example, if s = "abcd" and k = 3, the resulting string would be "abcdabcdabcd".
-
k is less than the length of s: In this case, we need to find a repeating substring of s that has length k. If such a substring does not exist, it is not possible to satisfy the above conditions and we should return an empty string. If such a substring exists, we can repeat it as many times as necessary to get the resulting string.
To find a repeating substring of s that has length k, we can use a sliding window approach. We start with a window of length k and slide it through the string s, checking at each position if the substring in the window is repeating. If the substring is repeating, we can repeat it as many times as necessary to get the resulting string. If the substring is not repeating, we slide the window one position to the right and try again.
If we slide the window all the way to the end of the string s and do not find a repeating substring, it is not possible to satisfy the above conditions and we should return an empty string.
Here is the detailed solution for the Construct String With Repeat Limit problem on leetcode:
- Create a variable
n
to store the length of the input strings
. - Create a variable
m
to store the value of input integerk
. - Check if k is greater than or equal to the length of s. If yes, then we can simply repeat the string s k times to get the resulting string. Return the resulting string.
- If k is less than the length of s, then we need to find a repeating substring of s that has length k.
- Create a dictionary
d
to store the frequency of each substring of length k ins
. - Create two pointer variables,
left
andright
, initialized to 0. - Create a variable
max_freq
to store the maximum frequency of any substring of length k ins
. - Create a variable
min_length
to store the length of the shortest resulting string that satisfies the above conditions. - Create a variable
result
to store the resulting string. - While
right
is less than n - k + 1:- Extract the substring of length k from
s
starting atright
. - If the frequency of this substring in
s
is greater thanmax_freq
, updatemax_freq
accordingly. - Put the frequency of this substring in the dictionary
d
. - Advance the right pointer by 1.
- Extract the substring of length k from
- Reset the
left
andright
pointers to 0. - While
right
is less than n - k + 1:- Extract the substring of length k from
s
starting atright
. - If the frequency of this substring in
s
is equal tomax_freq
, increment the frequency of this substring in the dictionaryd
. - While the frequency of the substring at
left
is greater than 1:- Extract the substring of length k from
s
starting atleft
. - Decrement the frequency of this substring in the dictionary
d
. - Advance the left pointer by k.
- Extract the substring of length k from
- If the frequency of the substring at
left
is 1 and the length of the resulting string is either 0 or greater thanright
-left
+ k, update the length of the resulting string and the variableresult
. - Advance the right pointer by k.
- Extract the substring of length k from
- Create a dictionary
- If
result
is still an empty string, return an empty string, else return the value ofresult
.
Time Complexity: O(n * k), where n is the length of the input string s and k is the value of the input integer k.
Space Complexity: O(n), for the dictionary d
created to store the frequency of each substring.
Construct String With Repeat Limit Solution Code
1