Given two strings str and patt, the task is to find the count of times patt can be formed using the characters of str.
Input: str = “geeksforgeeks”, patt = “geeks”
“geeks” can be made at most twice from
the characters of “geeksforgeeks”.
Input: str = “abcbca”, patt = “aabc”
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:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Check whether second string can be formed from characters of first string
- Largest sub-string where all the characters appear at least K times
- Remove characters from a String that appears exactly K times
- Lexicographically largest string formed from the characters in range L and R
- Remove characters from string that appears strictly less than K times
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Find the number of strings formed using distinct characters of a given string
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Count of strings that can be formed from another string using each character at-most once
- Count the minimum number of groups formed in a string
- Count of 1-bit and 2-bit characters in the given binary string
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Check if a string can be formed from another string by at most X circular clockwise shifts
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.