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:
CPP
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
struct Trie {
int index;
Trie* child[MAX_CHAR];
Trie()
{
for ( int i = 0; i < MAX_CHAR; i++)
child[i] = NULL;
index = -1;
}
};
void insert(Trie* root, string str, int index)
{
Trie* node = root;
for ( int i = 0; i < str.size(); i++) {
char ind = str[i] - 'a' ;
if (!node->child[ind])
node->child[ind] = new Trie();
node = node->child[ind];
}
node->index = index;
}
bool preorder(Trie* node, string arr[])
{
if (node == NULL)
return false ;
for ( int i = 0; i < MAX_CHAR; i++) {
if (node->child[i] != NULL) {
if (node->child[i]->index != -1)
cout << arr[node->child[i]->index] << endl;
preorder(node->child[i], arr);
}
}
}
void printSorted(string arr[], int n)
{
Trie* root = new Trie();
for ( int i = 0; i < n; i++)
insert(root, arr[i], i);
preorder(root, arr);
}
int main()
{
string arr[] = { "abc" , "xy" , "bcd" };
int n = sizeof (arr) / sizeof (arr[0]);
printSorted(arr, n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static final int MAX_CHAR = 26 ;
static class Trie {
int index;
Trie child[] = new Trie[MAX_CHAR];
Trie()
{
for ( int i = 0 ; i < MAX_CHAR; i++)
child[i] = null ;
index = - 1 ;
}
}
static void insert(Trie root, String str, int index)
{
Trie node = root;
for ( int i = 0 ; i < str.length(); i++) {
int ind = str.charAt(i) - 'a' ;
if (node.child[ind] == null )
node.child[ind] = new Trie();
node = node.child[ind];
}
node.index = index;
}
static boolean preorder(Trie node, String arr[])
{
if (node == null ) {
return false ;
}
for ( int i = 0 ; i < MAX_CHAR; i++) {
if (node.child[i] != null ) {
if (node.child[i].index != - 1 ) {
System.out.print(
arr[node.child[i].index] + " " );
}
preorder(node.child[i], arr);
}
}
return false ;
}
static void printSorted(String arr[], int n)
{
Trie root = new Trie();
for ( int i = 0 ; i < n; ++i) {
insert(root, arr[i], i);
}
preorder(root, arr);
}
public static void main(String[] args)
{
String arr[] = { "abc" , "xy" , "bcd" };
int n = arr.length;
printSorted(arr, n);
}
}
|
Python3
MAX_CHAR = 26
class Trie:
def __init__( self ):
self .child = [ None for i in range (MAX_CHAR)]
self .index = - 1
def insert(root, str ,index):
node = root
for i in range ( len ( str )):
ind = ord ( str [i]) - ord ( 'a' )
if (node.child[ind] = = None ):
node.child[ind] = Trie()
node = node.child[ind]
node.index = index
def preorder(node, arr):
if (node = = None ):
return False
for i in range (MAX_CHAR):
if (node.child[i] ! = None ):
if (node.child[i].index ! = - 1 ):
print (arr[node.child[i].index])
preorder(node.child[i], arr)
def printSorted(arr,n):
root = Trie()
for i in range (n):
insert(root, arr[i], i)
preorder(root, arr)
arr = [ "abc" , "xy" , "bcd" ]
n = len (arr)
printSorted(arr, n)
|
C#
using System;
class GFG
{
static int MAX_CHAR = 26;
public class Trie
{
public int index;
public Trie[] child = new Trie[MAX_CHAR];
public Trie()
{
for ( int i = 0; i < MAX_CHAR; i++)
child[i] = null ;
index = -1;
}
}
static void Insert(Trie root, string str, int index)
{
Trie node = root;
for ( int i = 0; i < str.Length; i++)
{
int ind = str[i] - 'a' ;
if (node.child[ind] == null )
node.child[ind] = new Trie();
node = node.child[ind];
}
node.index = index;
}
static void Preorder(Trie node, string [] arr)
{
if (node == null )
{
return ;
}
for ( int i = 0; i < MAX_CHAR; i++)
{
if (node.child[i] != null )
{
if (node.child[i].index != -1)
{
Console.WriteLine(arr[node.child[i].index] + " " );
}
Preorder(node.child[i], arr);
}
}
}
static void PrintSorted( string [] arr, int n)
{
Trie root = new Trie();
for ( int i = 0; i < n; i++)
{
Insert(root, arr[i], i);
}
Preorder(root, arr);
}
static void Main( string [] args)
{
string [] arr = { "abc" , "xy" , "bcd" };
int n = arr.Length;
PrintSorted(arr, n);
}
}
|
Javascript
<script>
const MAX_CHAR = 26;
class Trie {
constructor()
{
this .child = new Array(MAX_CHAR).fill( null );
this .index = -1;
}
}
function insert(root,str,index)
{
let node = root;
for (let i = 0; i < str.length; i++) {
let ind = str.charCodeAt(i) - 'a' .charCodeAt(0);
if (node.child[ind] == null )
node.child[ind] = new Trie();
node = node.child[ind];
}
node.index = index;
}
function preorder(node, arr)
{
if (node == null )
return false ;
for (let i = 0; i < MAX_CHAR; i++) {
if (node.child[i] != null ) {
if (node.child[i].index != -1)
document.write(arr[node.child[i].index], "</br>" );
preorder(node.child[i], arr);
}
}
}
function printSorted(arr,n)
{
let root = new Trie();
for (let i = 0; i < n; i++)
insert(root, arr[i], i);
preorder(root, arr);
}
let arr = [ "abc" , "xy" , "bcd" ];
let n = arr.length;
printSorted(arr, n);
</script>
|
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)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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!
Last Updated :
02 Mar, 2023
Like Article
Save Article