Sort the words in lexicographical order in Python
Given a strings, we need to sort the words in lexicographical order (dictionary order). Examples :
Input : "hello python program how are you"
Output : are
Input : "Coders loves the algorithms"
Output : Coders
Note: The words which have first letter is capital letter they will print according alphabetical manner.
Approach : Approach used in this program is very simple. Split the strings using split() function. After that sort the words in lexicographical order using sort(). Iterate the words through loop and print each word, which are already sorted.
( i )
"hello this is example how to sort "
"the word in alphabetical manner"
Time Complexity: O(nlogn) where n is the length of the string.
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!