Intuition

We need to sort the words in the sentence - so first split the sentence using the ' ' delimiter. Then extract the sorting order number with word[-1] and convert it to a int int(word[-1]).

Then while sorting, we use a comparator and pass the sorting order number to the lambda function.

Lambda functions in Python are amazing: learn more about them here

Code

Python3

def sortSentence(self, s: str) -> str:
    return ' '.join([x[:-1] for x in sorted(s.split(' '), key=lambda x: int(x[-1]))])

Big O Analysis

  • Runtime

    The runtime complexity here is since we are sorting the words in the sentence of length N.

  • Memory

    The memory usage is since are using the sorted function which returns a list.

— A

GitHub | Twitter