Given a string, find if it is possible to convert it to a string that is repetition of substring with k characters. To convert, we can replace one substring of length k with k characters.
Input: str = "bdac", k = 2 Output: True We can either replace "bd" with "ac" or "ac" with "bd". Input: str = "abcbedabcabc", k = 3 Output: True Replace "bed" with "abc" so that the whole string becomes repetition of "abc". Input: str = "bcacc", k = 3 Output: False k doesn't divide string length i.e. 5%3 != 0 Input: str = "bcacbcac", k = 2 Output: False Input: str = "bcdbcdabcedcbcd", k = 3 Output: False
This can be used in compression. If we have a string where complete string is repetition except one substring, then we can use this algorithm to compress the string.
One observation is, length of string must be a multiple of k as we can replace only one substring.
The idea is declare a map mp which maps strings of length k to an integer denoting its count. So, if there are only two different sub-strings of length k in the map container and count of one of the sub-string is 1 then answer is true. Otherwise answer is false.
This article is contributed by Himanshu Gupta(Bagri). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- K length words that can be formed from given characters without repetition
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Find length of longest subsequence of one string which is substring of another string
- Length of longest substring to be deleted to make a string equal to another string
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Print all permutations with repetition of characters
- Distributing all balls without repetition
- Maximum length substring with highest frequency in a string
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Substring of length K having maximum frequency in the given string
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum changes required to make first string substring of second string
- Queries to check if string B exists as substring in string A
- Check if the given string is shuffled substring of another string
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Lexicographically smallest and largest anagrams of a string containing another string as its substring