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 | Reverse Order Sort in String List
- 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 | Merge Tuple String List values to String
- Python - Remove front K characters from each string in String List
- Python - Length of shortest string in string list
- Python | Check if string ends with any string in given list
- Python | Sort each String in String 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
- Python User defined functions
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.