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 | All Permutations of a string in lexicographical order without using recursion
- Python | Check order of character in string using OrderedDict( )
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Merge Tuple String List values to String
- Python | Check if string ends with any string in given list
- Python | Check if a given string is binary string or not
- Python | Sort each String in String list
- String slicing in Python to rotate a string
- Sort an array of string of dates in ascending order
- Python User defined functions
- User-defined Exceptions in Python with Examples
- Python | Convert List of String List to String List
- Python | Scope resolution when a function is called or defined
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.