Convert to a string that is repetition of a substring of k length
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.
Examples:
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.
C++
// C++ program to check if a string can be converted to // a string that has repeated substrings of length k. #include<bits/stdc++.h> using namespace std; // Returns true if str can be coverted to a string // with k repeated substrings after replacing k // characters. bool checkString(string str, long k) { // Length of string must be a multiple of k int n = str.length(); if (n%k != 0) return false; // Map to store strings of length k and their counts unordered_map<string, int> mp; for (int i=0; i<n; i+=k) mp[str.substr(i, k)]++; // If string is already a repition of k substrings, // return true. if (mp.size() == 1) return true; // If number of distinct substrings is not 2, then // not possible to replace a string. if (mp.size() != 2) return false; // One of the two distinct must appear exactly once. // Either the first entry appears once, or it appears // n/k-1 times to make other substring appear once. if ((mp.begin()->second == (n/k - 1)) || mp.begin()->second == 1) return true; return false; } // Driver code int main() { checkString("abababcd", 2)? cout << "Yes" : cout << "No"; return 0; } |
Java
// Java program to check if a string // can be converted to a string that has // repeated substrings of length k. import java.util.HashMap; import java.util.Iterator; class GFG { // Returns true if str can be coverted // to a string with k repeated substrings // after replacing k characters. static boolean checkString(String str, int k) { // Length of string must be // a multiple of k int n = str.length(); if (n % k != 0) return false; // Map to store strings of // length k and their counts HashMap<String, Integer> mp = new HashMap<>(); try { for (int i = 0; i < n; i += k) mp.put(str.substring(i, k), mp.get(str.substring(i, k)) == null ? 1 : mp.get(str.substring(i, k)) + 1); } catch (Exception e) { } // If string is already a repition // of k substrings, return true. if (mp.size() == 1) return true; // If number of distinct substrings is not 2, // then not possible to replace a string. if (mp.size() != 2) return false; HashMap.Entry<String, Integer> entry = mp.entrySet().iterator().next(); // One of the two distinct must appear // exactly once. Either the first entry // appears once, or it appears n/k-1 times // to make other substring appear once. if (entry.getValue() == (n / k - 1) || entry.getValue() == 1) return true; return false; } // Driver code public static void main(String[] args) { if (checkString("abababcd", 2)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by // sanjeev2552 |
Python3
# Python3 program to check if a can be converted to # a that has repeated subs of length k. # Returns True if S can be coverted to a # with k repeated subs after replacing k # characters. def check( S, k): # Length of must be a multiple of k n = len(S) if (n % k != 0): return False # Map to store s of length k and their counts mp = {} for i in range(0, n, k): mp[S[i:k]] = mp.get(S[i:k], 0) + 1 # If is already a repition of k subs, # return True. if (len(mp) == 1): return True # If number of distinct subs is not 2, then # not possible to replace a . if (len(mp) != 2): return False # One of the two distinct must appear exactly once. # Either the first entry appears once, or it appears # n/k-1 times to make other sub appear once. for i in mp: if i == (n//k - 1) or mp[i] == 1: return True return False # Driver code if check("abababcd", 2): print("Yes") else: print("No") # This code is contributed by mohit kumar 29 |
Output:
Yes
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 contribute@geeksforgeeks.org. 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.
Recommended Posts:
- Find length of longest subsequence of one string which is substring of another string
- Maximum length substring with highest frequency in a string
- K length words that can be formed from given characters without repetition
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- 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
- Length of the longest valid substring
- Length of the longest substring with equal 1s and 0s
- Find max length odd parity substring
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest substring that do not contain any palindrome
- Maximum length substring having all same characters after k changes
- Minimum K such that every substring of length atleast K contains a character c
- Substring with highest frequency length product
- Length of the longest substring without repeating characters



