Given two strings S1 and S2(all characters are in lower-case). The task is to check if S2 can be formed from S1 using given constraints:
1. Characters of S2 is there in S1 if there are two ‘a’ in S2, then S1 should have two ‘a’ also.
2. If any character of S2 is not present in S1, check if the previous two ASCII characters are there in S1. e.g., if ‘e’ is there in S2 and not in S1, then ‘c’ and ‘d’ can be used from S1 to make ‘e’.
Note: All characters from S1 can be used only once.
Input: S= abbat, W= cat
‘c’ is formed from ‘a’ and ‘b’, ‘a’ and ‘t’ is present in S1.
Input: S= abbt, W= cat
‘c’ is formed from ‘a’ and ‘b’, but to form the next character
‘a’ in S2, there is no more unused ‘a’ left in S1.
Approach: The above problem can be solved using hashing. The count of all the characters in S1 is stored in a hash-table. Traverse in the string, and check if the character in S2 is there in the hash-table, reduce the count of that particular character in the hash-table. If the character is not there in the hash-table, check if the previous two ASCII characters are there in the hash-table, then reduce the count of the previous two ASCII characters in the hash-table. If all the characters can be formed from S1 using the given constraints, the string S2 can be formed from S1, else it cannot be formed.
Below is the implementation of the above approach:
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Count of strings that can be formed from another string using each character at-most once
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Check if a string can be converted to another by swapping of adjacent characters of given type
- Check if a string can be repeated to make another string
- Check if a string can be converted to another string by replacing vowels and consonants
- Construct a binary string following the given constraints
- Count occurrences of a string that can be constructed from another given string
- Check if the given string is shuffled substring of another string
- Maximum possible time that can be formed from four digits
- Minimum number of consecutive sequences that can be formed in an array
- Count of alphabets whose ASCII values can be formed with the digits of N
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Check if a string is substring of another
- Check if the large number formed is divisible by 41 or not
- Check if a number is formed by Concatenation of 1, 14 or 144 only
- Check whether the string can be printed using same row of qwerty keypad
- Check whether the string S1 can be made equal to S2 with the given operation
- Check if a number starts with another number or not
- Strings formed from given characters without any consecutive repeating characters
- Maximize 3rd element sum in quadruplet sets formed from given Array
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.