Given two strings, find if we can make first string from second by deleting some characters from second and rearranging remaining characters.
Input : s1 = ABHISHEKsinGH : s2 = gfhfBHkooIHnfndSHEKsiAnG Output : Possible Input : s1 = Hello : s2 = dnaKfhelddf Output : Not Possible Input : s1 = GeeksforGeeks : s2 = rteksfoGrdsskGeggehes Output : Possible
We basically need to find if one string contains characters which are subset of characters in second string. First we count occurrences of all characters in second string. Then we traverse through first string and reduce count of every character that is present in first. If at any moment, count becomes less than 0, we return false. If all counts remain greater than or equal to 0, we return true.
# Python 3 program to find if it is possible
# to make a string from characters present
# in other string.
MAX_CHAR = 256
# Returns true if it is possible to make
# s1 from characters present in s2.
def isPossible(s1, s2):
# Count occurrences of all characters
# present in s2..
count =  * MAX_CHAR
for i in range(len(s2)):
count[ord(s2[i])] += 1
# For every character, present in s1,
# reduce its count if count is more
# than 0.
for i in range(len(s1)):
if (count[ord(s1[i])] == 0):
count[ord(s1[i])] -= 1
# Driver code
if __name__ == “__main__”:
s1 = “GeeksforGeeks”
s2 = “rteksfoGrdsskGeggehes”
if (isPossible(s1, s2)):
# This code is contributed by ita_c
Time complexity is: O(n)
This article is contributed by Prabhat kumar singh. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Min flips of continuous characters to make all characters same in a string
- Missing characters to make a string Pangram
- Minimum characters to be added at front to make string palindrome
- Minimum number of characters to be removed to make a binary string alternate
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum replacements to make adjacent characters unequal in a ternary string
- Replace minimal number of characters to make all characters pair wise distinct
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- String slicing in Python to check if a string can become empty by recursive deletion
- Permutation of a string with maximum number of characters greater than its adjacent characters
- String with k distinct characters and no same characters adjacent
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Minimum operations to make frequency of all characters equal K
- Print the arranged positions of characters to make palindrome