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 occurrence 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.
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.
- Check if a string can be formed from another string using given constraints
- Count of sub-strings that contain character X at least once
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Map every character of one string to another such that all occurrences are mapped to the same character
- Count of strings that can be formed using a, b and c under given constraints
- Count ways to split array into two equal sum subarrays by replacing each array element to 0 once
- Check if the string contains consecutive letters and each letter occurs exactly once
- Check if two strings can be made equal by swapping one character among each other
- Count all sub-strings with weight of characters atmost K
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Maximize count of strings of length 3 that can be formed from N 1s and M 0s
- Count of times second string can be formed from the characters of first string
- All possible strings of any length that can be formed from a given string
- Check if given string can be formed by two other strings or their permutations
- Check if K palindromic strings can be formed from a given string
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Count occurrences of a string that can be constructed from another given string
- Count distinct strings possible by replacing each character by its Morse code
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.