Given two strings str1 and str2, the task is to find the maximum number of times str1 occurs in str2 as a non-overlapping substring after rearranging the characters of str2
Examples:
Input: str1 = “geeks”, str2 = “gskefrgoekees”
Output: 2
str = “geeksforgeeks”Input: str1 = “aa”, str2 = “aaaa”
Output: 2
Approach: The idea is to store the frequency of characters of both the strings and comparing them.
- If there is a character whose frequency in the first string is greater than its frequency in the second string then the answer is always 0 because string str1 can never occur in str2.
- After storing the frequency of the characters of both the strings, perform integer division between the non-zero frequency of characters of str1 and str2. The minimum value would be the answer.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int MAX = 26; // Function to return the maximum number // of times str1 can appear as a // non-overlapping substring in str2 int maxSubStr(string str1, int len1, string str2, int len2) { // str1 cannot never be substring of str2 if (len1 > len2) return 0; // Store the frequency of the characters of str1 int freq1[MAX] = { 0 }; for (int i = 0; i < len1; i++) freq1[str1[i] - 'a']++; // Store the frequency of the characters of str2 int freq2[MAX] = { 0 }; for (int i = 0; i < len2; i++) freq2[str2[i] - 'a']++; // To store the required count of substrings int minPoss = INT_MAX; for (int i = 0; i < MAX; i++) { // Current character doesn't appear in str1 if (freq1[i] == 0) continue; // Frequency of the current character in str1 // is greater than its frequency in str2 if (freq1[i] > freq2[i]) return 0; // Update the count of possible substrings minPoss = min(minPoss, freq2[i] / freq1[i]); } return minPoss; } // Driver code int main() { string str1 = "geeks", str2 = "gskefrgoekees"; int len1 = str1.length(); int len2 = str2.length(); cout << maxSubStr(str1, len1, str2, len2); return 0; } |
Java
// Java implementation of the approach class GFG { final static int MAX = 26; // Function to return the maximum number // of times str1 can appear as a // non-overlapping substring in str2 static int maxSubStr(char []str1, int len1, char []str2, int len2) { // str1 cannot never be substring of str2 if (len1 > len2) return 0; // Store the frequency of the characters of str1 int freq1[] = new int[MAX]; for (int i = 0; i < len1; i++) freq1[i] = 0; for (int i = 0; i < len1; i++) freq1[str1[i] - 'a']++; // Store the frequency of the characters of str2 int freq2[] = new int[MAX]; for (int i = 0; i < len2; i++) freq2[i] = 0; for (int i = 0; i < len2; i++) freq2[str2[i] - 'a']++; // To store the required count of substrings int minPoss = Integer.MAX_VALUE; for (int i = 0; i < MAX; i++) { // Current character doesn't appear in str1 if (freq1[i] == 0) continue; // Frequency of the current character in str1 // is greater than its frequency in str2 if (freq1[i] > freq2[i]) return 0; // Update the count of possible substrings minPoss = Math.min(minPoss, freq2[i] / freq1[i]); } return minPoss; } // Driver code public static void main (String[] args) { String str1 = "geeks", str2 = "gskefrgoekees"; int len1 = str1.length(); int len2 = str2.length(); System.out.println(maxSubStr(str1.toCharArray(), len1, str2.toCharArray(), len2)); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach import sys MAX = 26; # Function to return the maximum number # of times str1 can appear as a # non-overlapping sustring bin str2 def maxSubStr(str1, len1, str2, len2): # str1 cannot never be # substring of str2 if (len1 > len2): return 0; # Store the frequency of # the characters of str1 freq1 = [0] * MAX; for i in range(len1): freq1[ord(str1[i]) - ord('a')] += 1; # Store the frequency of # the characters of str2 freq2 = [0] * MAX; for i in range(len2): freq2[ord(str2[i]) - ord('a')] += 1; # To store the required count # of substrings minPoss = sys.maxsize; for i in range(MAX): # Current character doesn't appear # in str1 if (freq1[i] == 0): continue; # Frequency of the current character # in str1 is greater than its # frequency in str2 if (freq1[i] > freq2[i]): return 0; # Update the count of possible substrings minPoss = min(minPoss, freq2[i] / freq1[i]); return int(minPoss); # Driver code str1 = "geeks"; str2 = "gskefrgoekees"; len1 = len(str1); len2 = len(str2); print(maxSubStr(str1, len1, str2, len2)); # This code is contributed by 29AjayKumar |
C#
// C# implementation of the above approach using System; class GFG { readonly static int MAX = 26; // Function to return the maximum number // of times str1 can appear as a // non-overlapping substring in str2 static int maxSubStr(char []str1, int len1, char []str2, int len2) { // str1 cannot never be substring of str2 if (len1 > len2) return 0; // Store the frequency of the characters of str1 int []freq1 = new int[MAX]; for (int i = 0; i < len1; i++) freq1[i] = 0; for (int i = 0; i < len1; i++) freq1[str1[i] - 'a']++; // Store the frequency of the characters of str2 int []freq2 = new int[MAX]; for (int i = 0; i < len2; i++) freq2[i] = 0; for (int i = 0; i < len2; i++) freq2[str2[i] - 'a']++; // To store the required count of substrings int minPoss = int.MaxValue; for (int i = 0; i < MAX; i++) { // Current character doesn't appear in str1 if (freq1[i] == 0) continue; // Frequency of the current character in str1 // is greater than its frequency in str2 if (freq1[i] > freq2[i]) return 0; // Update the count of possible substrings minPoss = Math.Min(minPoss, freq2[i] / freq1[i]); } return minPoss; } // Driver code public static void Main (String[] args) { String str1 = "geeks", str2 = "gskefrgoekees"; int len1 = str1.Length; int len2 = str2.Length; Console.WriteLine(maxSubStr(str1.ToCharArray(), len1, str2.ToCharArray(), len2)); } } // This code is contributed by 29AjayKumar |
2
Time Complexity: O(max(M, N)) where M and N are the lengths of the given strings str1 and str2 respectively.
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.
Recommended Posts:
- Count of characters in str1 such that after deleting anyone of them str1 becomes str2
- Largest substring of str2 which is a prefix of str1
- Check whether str1 can be converted to str2 with the given operations
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
- Minimum cost to convert str1 to str2 with the given operations
- Transform string str1 into str2 by taking characters from string str3
- Find the element that appears once in an array where every other element appears twice
- First element that appears even number of times in an array
- Count number of times each Edge appears in all possible paths of a given Tree
- Find the only element that appears b times
- Check if max occurring character of one string appears same no. of times in other
- Remove characters from string that appears strictly less than K times
- Count all elements in the array which appears at least K times after their first occurrence
- Reduce the array such that each element appears at most 2 times
- Remove characters from a String that appears exactly K times
- Reduce the array such that each element appears at most K times
- Longest subsequence where every character appears at-least k times
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Maximized partitions of a string such that each character of the string appears in one substring
- Find if a given string can be represented from a substring by iterating the substring “n” times
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

