Given two strings str1 and str2, the task is to print the number of times str2 can be formed using characters of str1. However, a character at any index of str1 can only be used once in the formation of str2.
Input: str1 = “arajjhupoot”, str2 = “rajput”
str2 can only be formed once using characters of str1.
Input: str1 = “foreeksgekseg”, str2 = “geeks”
Approach: Since the problem has a restriction on using characters of str1 only once to form str2. If one character has been used to form one str2, it cannot be used in forming another str2. Every character of str2 must be present in str1 at least for the formation of one str1. If all the characters of str2 are already present in str1, then the character which has the minimum occurrence in str1 will be the number of str2’s that can be formed using the characters of str1 once. Below are the steps:
- Create an hash-array which stores the number of occurrences of each character of str.
- Iterate for all the characters of str2, and find the minimum most occurrences of every character in str1.
- Return the minimum occurence which will be the answer.
Below is the implementation of the above approach:
Time Complexity: O(max(l1,l2)), where l1 and l2 are length of str1 and str2 respectively.
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Count of strings that can be formed using a, b and c under given constraints
- Lexicographically smallest string formed by removing at most one character
- All possible strings of any length that can be formed from a given string
- Generate two output strings depending upon occurrence of character in input string.
- Generate two output strings depending upon occurrence of character in input string in Python
- Program to count occurrence of a given character in a string
- Count occurrences of a character in a repeated string
- Count of number of given string in 2D character array
- Count All Palindrome Sub-Strings in a String | Set 2
- Count All Palindrome Sub-Strings in a String | Set 1
- Count substrings that starts with character X and ends with character Y
- Replace every character of string by character whose ASCII value is K times more than it
- Find a string such that every character is lexicographically greater than its immediate next character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.