Given two strings (of lowercase letters), a pattern and a string. The task is to sort string according to the order defined by pattern. It may be assumed that pattern has all characters of the string and all characters in pattern appear only once.
Input : pat = "bca", str = "abc" Output : str = "bca" Input : pat = "bxyzca", str = "abcabcabc" Output : str = "bbbcccaaa" Input : pat = "wcyuogmlrdfphitxjakqvzbnes", str = "jcdokai" Output : str = "codijak"
The idea is to first count occurrences of all characters in str and store these counts in a count array. Then traverse pattern from left to right, and for each character pat[i], see how many times it appears in count array and copy this character these many times to str.
Below is the implementation of above idea.
Time complexity : O(m + n) where m is length of pattern and n is length of str.
Exercise : In the above solution, it is assumed that pattern has all characters of str. Consider a modified version where pattern may not have all characters and the task is to put all remaining characters (in string but not in pattern) at the end. The remaining characters need to be put in alphabetically sorted order.
Hint : In the second loop, when increase index and put character in str, we can also decrease count at that time. And finally we traverse the count array to put remaining characters in alphabetically sorted order.
This article is contributed by Sanjay Khadda. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
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.
- Check if string follows order of characters defined by a pattern or not | Set 1
- Check if string follows order of characters defined by a pattern or not | Set 2
- Check if string follows order of characters defined by a pattern or not | Set 3
- Remove duplicates from string keeping the order according to last occurrences
- Sort an array of strings according to string lengths
- Sort an array of strings according to string lengths using Map
- Sort a string according to the frequency of characters
- Check if words are sorted according to new order of alphabets
- Sort an Array of Strings according to the number of Vowels in them
- Sort an array of Strings according frequency
- Print array of strings in sorted order without copying one string into another
- Minimize steps defined by a string required to reach the destination from a given source
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Program to sort string in descending order
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Minimum characters required to be removed to sort binary string in ascending order
- Reverse String according to the number of words
- Find the last remaining Character in the Binary String according to the given conditions
- Reverse the substrings of the given String according to the given Array of indices