Given two strings str and patt, the task is to find the count of times patt can be formed using the characters of str.
Examples:
Input: str = “geeksforgeeks”, patt = “geeks”
Output: 2
“geeks” can be made at most twice from
the characters of “geeksforgeeks”.Input: str = “abcbca”, patt = “aabc”
Output: 1
Approach: Count the frequency of all the characters of str and patt and store them in arrays strFreq[] and pattFreq[] respectively. Now any character ch which appears in patt can be used in a maximum of strFreq[ch] / pattFreq[ch] words and the minimum of this value among all the characters of patt is the required 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 update the freq[] array // to store the frequencies of // all the characters of str void updateFreq(string str, int freq[]) { int len = str.length(); // Update the frequency of the characters for (int i = 0; i < len; i++) { freq[str[i] - 'a']++; } } // Function to return the maximum count // of times patt can be formed // using the characters of str int maxCount(string str, string patt) { // To store the frequencies of // all the characters of str int strFreq[MAX] = { 0 }; updateFreq(str, strFreq); // To store the frequencies of // all the characters of patt int pattFreq[MAX] = { 0 }; updateFreq(patt, pattFreq); // To store the result int ans = INT_MAX; // For every character for (int i = 0; i < MAX; i++) { // If the current character // doesn't appear in patt if (pattFreq[i] == 0) continue; // Update the result ans = min(ans, strFreq[i] / pattFreq[i]); } return ans; } // Driver code int main() { string str = "geeksforgeeks"; string patt = "geeks"; cout << maxCount(str, patt); return 0; } |
Java
// Java implementation of the approach class GFG { static int MAX = 26; // Function to update the freq[] array // to store the frequencies of // all the characters of str static void updateFreq(String str, int freq[]) { int len = str.length(); // Update the frequency of the characters for (int i = 0; i < len; i++) { freq[str.charAt(i) - 'a']++; } } // Function to return the maximum count // of times patt can be formed // using the characters of str static int maxCount(String str, String patt) { // To store the frequencies of // all the characters of str int []strFreq = new int[MAX]; updateFreq(str, strFreq); // To store the frequencies of // all the characters of patt int []pattFreq = new int[MAX]; updateFreq(patt, pattFreq); // To store the result int ans = Integer.MAX_VALUE; // For every character for (int i = 0; i < MAX; i++) { // If the current character // doesn't appear in patt if (pattFreq[i] == 0) continue; // Update the result ans = Math.min(ans, strFreq[i] / pattFreq[i]); } return ans; } // Driver code public static void main(String[] args) { String str = "geeksforgeeks"; String patt = "geeks"; System.out.print(maxCount(str, patt)); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach MAX = 26 # Function to update the freq[] array # to store the frequencies of # all the characters of strr def updateFreq(strr, freq): lenn = len(strr) # Update the frequency of the characters for i in range(lenn): freq[ord(strr[i]) - ord('a')] += 1 # Function to return the maximum count # of times patt can be formed # using the characters of strr def maxCount(strr, patt): # To store the frequencies of # all the characters of strr strrFreq = [0 for i in range(MAX)] updateFreq(strr, strrFreq) # To store the frequencies of # all the characters of patt pattFreq = [0 for i in range(MAX)] updateFreq(patt, pattFreq) # To store the result ans = 10**9 # For every character for i in range(MAX): # If the current character # doesn't appear in patt if (pattFreq[i] == 0): continue # Update the result ans = min(ans, strrFreq[i] // pattFreq[i]) return ans # Driver code strr = "geeksforgeeks"patt = "geeks" print(maxCount(strr, patt)) # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG { static int MAX = 26; // Function to update the []freq array // to store the frequencies of // all the characters of str static void updateFreq(String str, int []freq) { int len = str.Length; // Update the frequency of the characters for (int i = 0; i < len; i++) { freq[str[i] - 'a']++; } } // Function to return the maximum count // of times patt can be formed // using the characters of str static int maxCount(String str, String patt) { // To store the frequencies of // all the characters of str int []strFreq = new int[MAX]; updateFreq(str, strFreq); // To store the frequencies of // all the characters of patt int []pattFreq = new int[MAX]; updateFreq(patt, pattFreq); // To store the result int ans = int.MaxValue; // For every character for (int i = 0; i < MAX; i++) { // If the current character // doesn't appear in patt if (pattFreq[i] == 0) continue; // Update the result ans = Math.Min(ans, strFreq[i] / pattFreq[i]); } return ans; } // Driver code public static void Main(String[] args) { String str = "geeksforgeeks"; String patt = "geeks"; Console.Write(maxCount(str, patt)); } } // This code is contributed by 29AjayKumar |
2
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:
- Check whether second string can be formed from characters of first string
- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Remove characters from the first string which are present in the second string
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Count of Substrings that can be formed without using the given list of Characters
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Count subsequences in first string which are anagrams of the second string
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Strings formed from given characters without any consecutive repeating characters
- K length words that can be formed from given characters without repetition
- Number of ways in which the substring in range [L, R] can be formed using characters out of the range
- Count of strings in the first array which are smaller than every string in the second array
- Minimum changes required to make first string substring of second string
- Most frequent word in first String which is not present in second String
- Find the character in first string that is present at minimum index in second string
- Count elements present in first array but not in second
- Count distinct elements after adding each element of First Array with Second Array
- Count number of rotated strings which have more number of vowels in the first half than second half
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.

