VOOZH about

URL: https://www.geeksforgeeks.org/dsa/make-string-another-deletion-rearrangement-characters/

⇱ Make a string from another by deletion and rearrangement of characters - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Make a string from another by deletion and rearrangement of characters

Last Updated : 21 Sep, 2022

Given two strings, find if we can make first string from second by deleting some characters from second and rearranging remaining characters.

Examples: 

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. 

Implementation:


Output
Possible

Time complexity is: O(n)
Auxiliary Space: O(256)

Comment
Article Tags:
Article Tags: