Given two strings (of lowercase letters), a pattern and a string. The task is to sort string according to the order defined by pattern and return the reverse of it. It may be assumed that pattern has all characters of the string and all characters in pattern appear only once.
Input : pat = "asbcklfdmegnot", str = "eksge" Output : str = "geeks" (after sorting, str becomes "skeeg" and return its reverse) Input : pat = "mgewqnasibkldjxruohypzcftv", str = "niocgd" Output : str = "coding"
The idea is to first maintain a dictionary according to the index provided in Pattern and then passing the lambda function(which uses utility of dictionary) into the sort function.
Below is the implementation of above idea.
Time Complexity: n*log(n) where n is the length of the string
- Python | Inverse Sorting String
- Python | Check order of character in string using OrderedDict( )
- Python | All Permutations of a string in lexicographical order without using recursion
- Python | Check if given string can be formed by concatenating string elements of list
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if string ends with any string in given list
- Python | Check if a given string is binary string or not
- String slicing in Python to rotate a string
- Sort an array of string of dates in ascending order
- User-defined Exceptions in Python with Examples
- Python | Scope resolution when a function is called or defined
- Python String | max()
- Python String | min()
- Python String
- Python | Add one string to another
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.