![]() |
VOOZH | about |
Given a string s and a positive integer K, the task is to find the length of the longest contiguous substring that appears at least K times in s.
Examples:
Input: s = "abababcdabcd", K = 2
Output: 4
Explanation: "abcd" is the longest repeating substring at least K times.Input: s = "aacd", K = 4
Output: 0
Approach: This can be solved with the following idea:
We will use the Rabin-Karp algorithm to solve this problem, which is a rolling hash-based algorithm to find the longest common substring that repeats at least k times.
Below are the steps:
Below is the implementation of the above code:
4
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)