Check if a string can be formed from another string using given constraints
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.
Examples:
Input: S= abbat, W= cat
Output: YES
‘c’ is formed from ‘a’ and ‘b’, ‘a’ and ‘t’ is present in S1.Input: S= abbt, W= cat
Output: NO
‘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:
C++
// CPP program to Check if a given // string can be formed from another // string using given constraints #include <bits/stdc++.h> using namespace std; // Function to check if S2 can be formed of S1 bool check(string S1, string S2) { // length of strings int n1 = S1.size(); int n2 = S2.size(); // hash-table to store count unordered_map<int, int> mp; // store count of each character for (int i = 0; i < n1; i++) { mp[S1[i]]++; } // traverse and check for every character for (int i = 0; i < n2; i++) { // if the character of s2 is present in s1 if (mp[S2[i]]) { mp[S2[i]]--; } // if the character of s2 is not present in // S1, then check if previous two ASCII characters // are present in S1 else if (mp[S2[i] - 1] && mp[S2[i] - 2]) { mp[S2[i] - 1]--; mp[S2[i] - 2]--; } else { return false; } } return true; } // Driver Code int main() { string S1 = "abbat"; string S2 = "cat"; // Calling function to check if (check(S1, S2)) cout << "YES"; else cout << "NO"; } |
Python3
# Python3 program to Check if a given string # can be formed from another string using # given constraints from collections import defaultdict # Function to check if S2 can # be formed of S1 def check(S1, S2): # length of strings n1 = len(S1) n2 = len(S2) # hash-table to store count mp = defaultdict(lambda:0) # store count of each character for i in range(0, n1): mp[S1[i]] += 1 # traverse and check for every character for i in range(0, n2): # if the character of s2 is # present in s1 if mp[S2[i]]: mp[S2[i]] -= 1 # if the character of s2 is not present # in S1, then check if previous two ASCII # characters are present in S1 elif (mp[chr(ord(S2[i]) - 1)] and mp[chr(ord(S2[i]) - 2)]): mp[chr(ord(S2[i]) - 1)] -= 1 mp[chr(ord(S2[i]) - 2)] -= 1 else: return False return True # Driver Code if __name__ == "__main__": S1 = "abbat" S2 = "cat" # Calling function to check if check(S1, S2): print("YES") else: print("NO") # This code is contributed by Rituraj Jain |
YES
Recommended Posts:
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Construct a binary string following the given constraints
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be repeated to make another string
- Count of strings that can be formed from another string using each character at-most once
- Check if a string is the typed name of the given name
- Check if the given string is K-periodic
- Check if a string is substring of another
- Quick way to check if all the characters of a string are same
- Check whether the string S1 can be made equal to S2 with the given operation
- Check if a binary string has two consecutive occurrences of one everywhere
- Check if a given string is made up of two alternating characters
- Check if string can be made lexicographically smaller by reversing any substring
- Print the longest prefix of the given string which is also the suffix of the same string
- Minimum changes required to make first string substring of 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 contribute@geeksforgeeks.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.
Improved By : rituraj_jain



