Monday, October 3, 2022
HomeSoftware DevelopmentType an Array of Strings in Lexicographical order

Type an Array of Strings in Lexicographical order


View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Given an array of strings arr[] of size N, the duty is to type the strings in Lexicographical order.

Examples:

Enter: arr[] = {“batman”, “bat”, “apple”} 
Output: 
apple
bat 
batman
Clarification: 
The lexicographical order of string is “apple”, “bat”, “batman”

Enter: arr[] = {“geeks”, “for”, “geeksforgeeks”} 
Output: 
for
geeks
geeksforgeeks

Method:

The array could be sorted in rising order by evaluating ASCII values of the leftmost character which are completely different among the many strings.

To type the strings we will use the inbuilt type() operate. The operate types the string in lexicographical order by default.

Under is the implementation for the above strategy:

C++

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

void Printlexiographically(int N, string arr[])

{

    

    type(arr, arr + N);

  

    for (int i = 0; i < N; i++) {

  

        

        cout << arr[i] << 'n';

    }

}

  

int fundamental()

{

    string arr[] = { "batman", "bat", "apple" };

    int N = sizeof(arr) / sizeof(arr[0]);

  

    

    Printlexiographically(N, arr);

  

    return 0;

}

Time Complexity: O(N * logN * M), the place M is the common size of the strings
Auxiliary House: O(1)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments