VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sorting-array-strings-words-using-trie/

⇱ Sorting array of strings (or words) using Trie - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sorting array of strings (or words) using Trie

Last Updated : 2 Mar, 2023

Given an array of strings, print them in alphabetical (dictionary) order. If there are duplicates in input array, we need to print them only once.

Examples: 

Input : "abc", "xy", "bcd"
Output : abc bcd xy 

Input : "geeks", "for", "geeks", "a", "portal", 
 "to", "learn", "can", "be", "computer", 
 "science", "zoom", "yup", "fire", "in", "data"
Output : a be can computer data fire for geeks
 in learn portal science to yup zoom

Trie is an efficient data structure used for storing data like strings. To print the string in alphabetical order we have to first insert in the trie and then perform preorder traversal to print in alphabetical order.

Implementation:


Output
abc
bcd
xy

Time Complexity: O(n*m) where n is the length of the array and m is the length of the longest word.
Auxiliary Space: O(n*m)

Comment