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:

  1. 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".

  2. 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:

  1. Create a variable n to store the length of the input string s.
  2. Create a variable m to store the value of input integer k.
  3. 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.
  4. 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 in s.
    • Create two pointer variables, left and right, initialized to 0.
    • Create a variable max_freq to store the maximum frequency of any substring of length k in s.
    • 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 at right.
      • If the frequency of this substring in s is greater than max_freq, update max_freq accordingly.
      • Put the frequency of this substring in the dictionary d.
      • Advance the right pointer by 1.
    • Reset the left and right pointers to 0.
    • While right is less than n - k + 1:
      • Extract the substring of length k from s starting at right.
      • If the frequency of this substring in s is equal to max_freq, increment the frequency of this substring in the dictionary d.
      • While the frequency of the substring at left is greater than 1:
        • Extract the substring of length k from s starting at left.
        • Decrement the frequency of this substring in the dictionary d.
        • Advance the left pointer by k.
      • If the frequency of the substring at left is 1 and the length of the resulting string is either 0 or greater than right - left + k, update the length of the resulting string and the variable result.
      • Advance the right pointer by k.
  5. If result is still an empty string, return an empty string, else return the value of result.

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