Check if any permutation of string is a K times repeated string
Given a string S and an integer K, the task is to check that if any permutation of the string can be formed by K times repeating any other string.
Examples:
Input: S = “abba”, K = 2
Output: Yes
Explanation:
Permutations of given string –
{“aabb”, “abab”, “abba”, “baab”, “baba”, “bbaa”}
As “abab” is repeating string of “ab”+”ab” = “abab”, which is also permutation of string.
Input: S = “abcabd”, K = 2
Output: No
Explanation:
There is no such repeating string in all permutations of the given string.
Approach 1: The idea is to find the frequency of each character of the string and check that the frequency of the character is a multiple of the given integer K. If the frequency of all characters of the string is divisible by K, then there is a string which is a permutation of the given string and also a K times repeated string.
Below is the implementation of the above approach:
C++
// C++ implementation to check that // the permutation of the given string // is K times repeated string #include <bits/stdc++.h> using namespace std; // Function to check that permutation // of the given string is a // K times repeating String bool repeatingString(string s, int n, int k) { // if length of string is // not divisible by K if (n % k != 0) { return false ; } // Frequency Array int frequency[123]; // Initially frequency of each // character is 0 for ( int i = 0; i < 123; i++) { frequency[i] = 0; } // Computing the frequency of // each character in the string for ( int i = 0; i < n; i++) { frequency[s[i]]++; } int repeat = n / k; // Loop to check that frequency of // every character of the string // is divisible by K for ( int i = 0; i < 123; i++) { if (frequency[i] % repeat != 0) { return false ; } } return true ; } // Driver Code int main() { string s = "abcdcba" ; int n = s.size(); int k = 3; if (repeatingString(s, n, k)) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; } |
Java
// Java implementation to check that // the permutation of the given String // is K times repeated String class GFG{ // Function to check that permutation // of the given String is a // K times repeating String static boolean repeatingString(String s, int n, int k) { // if length of String is // not divisible by K if (n % k != 0 ) { return false ; } // Frequency Array int []frequency = new int [ 123 ]; // Initially frequency of each // character is 0 for ( int i = 0 ; i < 123 ; i++) { frequency[i] = 0 ; } // Computing the frequency of // each character in the String for ( int i = 0 ; i < n; i++) { frequency[s.charAt(i)]++; } int repeat = n / k; // Loop to check that frequency of // every character of the String // is divisible by K for ( int i = 0 ; i < 123 ; i++) { if (frequency[i] % repeat != 0 ) { return false ; } } return true ; } // Driver Code public static void main(String[] args) { String s = "abcdcba" ; int n = s.length(); int k = 3 ; if (repeatingString(s, n, k)) { System.out.print( "Yes" + "\n" ); } else { System.out.print( "No" + "\n" ); } } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation to check that # the permutation of the given string # is K times repeated string # Function to check that permutation # of the given string is a # K times repeating String def repeatingString(s, n, k): # If length of string is # not divisible by K if (n % k ! = 0 ): return False # Frequency Array frequency = [ 0 for i in range ( 123 )] # Initially frequency of each # character is 0 for i in range ( 123 ): frequency[i] = 0 # Computing the frequency of # each character in the string for i in range (n): frequency[s[i]] + = 1 repeat = n / / k # Loop to check that frequency of # every character of the string # is divisible by K for i in range ( 123 ): if (frequency[i] % repeat ! = 0 ): return False return True # Driver Code if __name__ = = '__main__' : s = "abcdcba" n = len (s) k = 3 if (repeatingString(s, n, k)): print ( "Yes" ) else : print ( "No" ) # This code is contributed by Samarth |
C#
// C# implementation to check that // the permutation of the given String // is K times repeated String using System; class GFG{ // Function to check that permutation // of the given String is a // K times repeating String static bool repeatingString(String s, int n, int k) { // if length of String is // not divisible by K if (n % k != 0) { return false ; } // Frequency Array int []frequency = new int [123]; // Initially frequency of each // character is 0 for ( int i = 0; i < 123; i++) { frequency[i] = 0; } // Computing the frequency of // each character in the String for ( int i = 0; i < n; i++) { frequency[s[i]]++; } int repeat = n / k; // Loop to check that frequency of // every character of the String // is divisible by K for ( int i = 0; i < 123; i++) { if (frequency[i] % repeat != 0) { return false ; } } return true ; } // Driver Code public static void Main(String[] args) { String s = "abcdcba" ; int n = s.Length; int k = 3; if (repeatingString(s, n, k)) { Console.Write( "Yes" + "\n" ); } else { Console.Write( "No" + "\n" ); } } } // This code is contributed by Rajput-Ji |
Javascript
<script> // JavaScript implementation to check that // the permutation of the given string // is K times repeated string // Function to check that permutation // of the given string is a // K times repeating String function repeatingString( s, n, k) { // if length of string is // not divisible by K if (n % k != 0) { return false ; } // Frequency Array var frequency = new Array(123); // Initially frequency of each // character is 0 for (let i = 0; i < 123; i++) { frequency[i] = 0; } // Computing the frequency of // each character in the string for (let i = 0; i < n; i++) { frequency[s[i]]++; } var repeat = n / k; // Loop to check that frequency of // every character of the string // is divisible by K for (let i = 0; i < 123; i++) { if (frequency[i] % repeat != 0) { return false ; } } return true ; } // Driver Code var s = "abcdcba" ; var n = s.length; var k = 3; if (repeatingString(s, n, k)) { console.log( "Yes" ); } else { console.log( "No" ); } // This code is contributed by ukasp. </script> |
No
Performance Analysis:
Time Complexity O(N)
Auxiliary Space: O(1)
Approach 2 :
We can check for all possible substrings of the given string and then check if any of these substrings can be repeated K times to form a permutation of the original string.
- For each substring of length l = n/K, we can check if all characters in the substring have the same frequency (count) in the original string S. If this condition is not satisfied, we move to the next substring.
- We repeat step 1 for all substrings of length l in the original string S. If we find any substring which satisfies the condition, we return “Yes”, else we return “No”.
Below is the code for above approach :
C++
#include <bits/stdc++.h> using namespace std; // Function to check if a repeating substring of given length // exists in the given string bool isRepeatingSubstring(string s, int len) { int n = s.length(); // Check if given substring length is greater than string length if (len >= n) { return false ; } // Map to store the frequency of characters unordered_map< char , int > freq; // Find the frequency of characters in first len characters of the string for ( int i = 0; i < len; i++) { freq[s[i]]++; } // Check if the substring consisting of first len characters is repeating bool repeating = true ; for ( auto it : freq) { if (it.second != n/len) { repeating = false ; break ; } } // If the substring is not repeating, check for other substrings if (!repeating) { for ( int i = len; i < n; i++) { // Update the frequency map freq[s[i-len]]--; freq[s[i]]++; // Check if the substring consisting of previous len characters is repeating if (freq[s[i-len]] == 0) { freq.erase(s[i-len]); } if (freq.size() == 1 && freq.begin()->second == n/len) { return true ; } } } return false ; } // Driver code int main() { string S = "abba" ; int K = 2; if (isRepeatingSubstring(S, K)) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; } |
Java
//code in Java for the above approach import java.util.HashMap; import java.util.Map; public class Main { // Function to check if a repeating substring of given length // exists in the given string public static boolean isRepeatingSubstring(String s, int len) { int n = s.length(); // Check if given substring length is greater than string length if (len >= n) { return false ; } // Map to store the frequency of characters Map<Character, Integer> freq = new HashMap<>(); // Find the frequency of characters in the first len characters of the string for ( int i = 0 ; i < len; i++) { char ch = s.charAt(i); freq.put(ch, freq.getOrDefault(ch, 0 ) + 1 ); } // Check if the substring consisting of the first len characters is repeating boolean repeating = true ; for (Map.Entry<Character, Integer> entry : freq.entrySet()) { if (entry.getValue() != n / len) { repeating = false ; break ; } } // If the substring is not repeating, check for other substrings if (!repeating) { for ( int i = len; i < n; i++) { char prevChar = s.charAt(i - len); char currentChar = s.charAt(i); // Update the frequency map freq.put(prevChar, freq.get(prevChar) - 1 ); if (freq.get(prevChar) == 0 ) { freq.remove(prevChar); } freq.put(currentChar, freq.getOrDefault(currentChar, 0 ) + 1 ); // Check if the substring consisting of the previous len characters is repeating if (freq.size() == 1 && freq.values().iterator().next() == n / len) { return true ; } } } return false ; } // Driver code public static void main(String[] args) { String S = "abba" ; int K = 2 ; if (isRepeatingSubstring(S, K)) { System.out.println( "Yes" ); } else { System.out.println( "No" ); } } } |
Python
# Function to check if a repeating substring of a given length # exists in the given string def isRepeatingSubstring(s, length): n = len (s) # Check if the given substring length is greater than or equal to the string length if length > = n: return False # Dictionary to store the frequency of characters freq = {} # Find the frequency of characters in the first 'length' characters of the string for i in range (length): if s[i] in freq: freq[s[i]] + = 1 else : freq[s[i]] = 1 # Check if the substring consisting of the first 'length' characters is repeating repeating = True for count in freq.values(): if count ! = n / / length: repeating = False break # If the substring is not repeating, check for other substrings if not repeating: for i in range (length, n): # Update the frequency dictionary freq[s[i - length]] - = 1 if freq[s[i - length]] = = 0 : del freq[s[i - length]] if s[i] in freq: freq[s[i]] + = 1 else : freq[s[i]] = 1 # Check if the substring consisting of the previous 'length' characters is repeating if len (freq) = = 1 and list (freq.values())[ 0 ] = = n / / length: return True return False # Driver code S = "abba" K = 2 if isRepeatingSubstring(S, K): print ( "Yes" ) else : print ( "No" ) |
C#
using System; using System.Collections.Generic; class GFG { static bool IsRepeatingSubstring( string s, int len) { int n = s.Length; // Check if given substring length is greater than string length if (len >= n) { return false ; } // Dictionary to store the frequency of characters Dictionary< char , int > freq = new Dictionary< char , int >(); // Find the frequency of characters in the // first len characters of the string for ( int i = 0; i < len; i++) { if (freq.ContainsKey(s[i])) { freq[s[i]]++; } else { freq[s[i]] = 1; } } // Check if the substring consisting of // the first len characters is repeating bool repeating = true ; foreach ( var kvp in freq) { if (kvp.Value != n / len) { repeating = false ; break ; } } // If the substring is not repeating // check for other substrings if (!repeating) { for ( int i = len; i < n; i++) { // Update the frequency dictionary freq[s[i - len]]--; if (freq[s[i - len]] == 0) { freq.Remove(s[i - len]); } if (freq.ContainsKey(s[i])) { freq[s[i]]++; } else { freq[s[i]] = 1; } // Check if the substring consisting of the previous len characters is repeating if (freq.Count == 1 && freq.ContainsValue(n / len)) { return true ; } } } return false ; } static void Main( string [] args) { string S = "abba" ; int K = 2; if (IsRepeatingSubstring(S, K)) { Console.WriteLine( "Yes" ); } else { Console.WriteLine( "No" ); } } } |
Javascript
function isRepeatingSubstring(s, len) { const n = s.length; // Check if given substring length is // greater than string length if (len >= n) { return false ; } // Map to store the frequency of characters const freq = new Map(); // Find the frequency of characters in the first // len characters of the string for (let i = 0; i < len; i++) { const char = s[i]; freq.set(char, (freq.get(char) || 0) + 1); } // Check if the substring consisting of the // first len characters is repeating let repeating = true ; for (const [char, count] of freq) { if (count !== n / len) { repeating = false ; break ; } } // If the substring is not repeating // check for other substrings if (!repeating) { for (let i = len; i < n; i++) { // Update the frequency map const charToRemove = s[i - len]; const charToAdd = s[i]; freq.set(charToRemove, freq.get(charToRemove) - 1); if (freq.get(charToRemove) === 0) { freq. delete (charToRemove); } freq.set(charToAdd, (freq.get(charToAdd) || 0) + 1); // Check if the substring consisting of the previous // len characters is repeating if (freq.size === 1 && [...freq.values()][0] === n / len) { return true ; } } } return false ; } // Driver code const S = "abba" ; const K = 2; if (isRepeatingSubstring(S, K)) { console.log( "Yes" ); } else { console.log( "No" ); } |
Output :
Yes
Time Complexity : O(n^2)
Auxiliary Space : O(1)