Given two strings str1 and str2, check if str2 can be formed from str1
Input : str1 = geekforgeeks, str2 = geeks Output : Yes Here, string2 can be formed from string1. Input : str1 = geekforgeeks, str2 = and Output : No Here string2 cannot be formed from string1. Input : str1 = geekforgeeks, str2 = geeeek Output : Yes Here string2 can be formed from string1 as string1 contains 'e' comes 4 times in string2 which is present in string1.
The idea is to count frequencies of characters of str1 in a count array. Then traverse str2 and decrease frequency of characters of str2 in the count array. If frequency of a characters becomes negative at any point, return false.
Below is the implementation of above approach :
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.
- Count of times second string can be formed from the characters of first string
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Check whether Arithmetic Progression can be formed from the given array
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- 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
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Strings formed from given characters without any consecutive repeating characters
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- 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 Substrings that can be formed without using the given list of Characters
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if the given string of words can be formed from words present in the dictionary
- 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
- Find the character in first string that is present at minimum index in second 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.