Python | Sorting string using order defined by another string
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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course