Reorder the position of the words in alphabetical order
Last Updated :
12 Apr, 2023
Given an array arr[] of strings, the task is to reorder the strings lexicographically and print their positions in the original list.
Examples:
Input: arr[] = {“zxc”, “efg”, “jkl”}
Output: 2 3 1
The sorted list will be {“efg”, “jkl”, “zxc”} and their
original positions were 2, 3 and 1 respectively.
Input: arr[] = {“live”, “place”, “travel”, “word”, “sky”}
Output: 1 2 5 3 4
Approach: Assign all the words with an integer number equal to their position in the array. Then they sort the list of words lexicographically and their positions are altered, and therefore, their positions are printed starting from the first word in the sorted list.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void reArrange(string words[], int n)
{
map<string, int > mp;
for ( int i = 0; i < n; i++)
mp[words[i]] = i + 1;
sort(words, words + n);
for ( int i = 0; i < n; i++)
cout << mp[words[i]] << " " ;
}
int main()
{
string words[] = { "live" , "place" , "travel" , "word" , "sky" };
int n = sizeof (words) / sizeof (words[0]);
reArrange(words, n);
}
|
Java
import java.util.*;
class GFG {
static void reArrange(String words[], int n)
{
HashMap<String, Integer> freq = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
freq.put(words[i], (i + 1 ));
}
Arrays.sort(words);
for ( int i = 0 ; i < n; i++)
System.out.print(freq.get(words[i]) + " " );
}
public static void main(String[] args)
{
String words[] = { "live" , "place" , "travel" , "word" , "sky" };
int n = words.length;
reArrange(words, n);
}
}
|
Python3
def reArrange(words, n):
mp = {}
for i in range (n):
mp[words[i]] = i + 1
words.sort();
for i in range (n):
print (mp[words[i]], end = " " )
words = [ "live" , "place" , "travel" , "word" , "sky" ]
n = len (words)
reArrange(words, n);
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void reArrange(String[] words, int n)
{
Dictionary<String, int > freq = new Dictionary<String, int >();
for ( int i = 0; i < n; i++) {
freq.Add(words[i], (i + 1));
}
Array.Sort(words);
for ( int i = 0; i < n; i++)
Console.Write(freq[words[i]] + " " );
}
public static void Main(String[] args)
{
String[] words = { "live" , "place" , "travel" , "word" , "sky" };
int n = words.Length;
reArrange(words, n);
}
}
|
PHP
<?php
function reArrange( $words , $n )
{
$freq = array ();
for ( $i = 0; $i < $n ; $i ++)
{
$freq [ $words [ $i ]] = ( $i + 1) ;
}
sort( $words );
for ( $i = 0; $i < $n ; $i ++)
echo $freq [ $words [ $i ]], " " ;
}
$words = array ( "live" , "place" , "travel" , "word" , "sky" );
$n = count ( $words );
reArrange( $words , $n );
?>
|
Javascript
<script>
function reArrange(words, n)
{
var mp = new Map();
for ( var i = 0; i < n; i++)
mp.set(words[i], i + 1);
words.sort();
for ( var i = 0; i < n; i++)
{
document.write(mp.get(words[i])+ " " );
}
}
var words = [ "live" , "place" , "travel" , "word" , "sky" ];
var n = words.length;
reArrange(words, n);
</script>
|
Time Complexity: O(n * log n)
Auxiliary Space: O(n)
Approach:
The approach is to create a vector of integers representing the original indices of the strings, sort the vector of indices based on the strings using a lambda function that compares the corresponding strings, and then print the sorted vector of indices.
Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool compare(string a, string b) { return a < b; }
void reArrange(string words[], int n)
{
vector< int > indices(n);
for ( int i = 0; i < n; i++) {
indices[i] = i + 1;
}
sort(indices.begin(), indices.end(), [&]( int a, int b) {
return compare(words[a - 1], words[b - 1]);
});
for ( auto index : indices) {
cout << index << " " ;
}
}
int main()
{
string words[]
= { "live" , "place" , "travel" , "word" , "sky" };
int n = sizeof (words) / sizeof (words[0]);
reArrange(words, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static boolean compare(String a, String b)
{
return a.compareTo(b) < 0 ;
}
public static void reArrange(String[] words)
{
Integer[] indices = new Integer[words.length];
for ( int i = 0 ; i < words.length; i++) {
indices[i] = i + 1 ;
}
Arrays.sort(
indices,
(a, b)
-> compare(words[a - 1 ], words[b - 1 ]) ? - 1
: 1 );
for (Integer index : indices) {
System.out.print(index + " " );
}
}
public static void main(String[] args)
{
String[] words
= { "live" , "place" , "travel" , "word" , "sky" };
reArrange(words);
}
}
|
Python3
def reArrange(words):
n = len (words)
indices = [i + 1 for i in range (n)]
indices.sort(key = lambda x: words[x - 1 ])
for index in indices:
print (index, end = ' ' )
words = [ "live" , "place" , "travel" , "word" , "sky" ]
reArrange(words)
|
C#
using System;
using System.Linq;
public class GFG {
static bool Compare( string a, string b)
{
return a.CompareTo(b) < 0;
}
static void reArrange( string [] words, int n)
{
int [] indices = Enumerable.Range(1, n).ToArray();
Array.Sort(indices, (a, b) = > Compare(words[a - 1],
words[b - 1])
? -1
: 1);
Console.WriteLine( string .Join( " " , indices));
}
public static void Main()
{
string [] words
= { "live" , "place" , "travel" , "word" , "sky" };
int n = words.Length;
reArrange(words, n);
}
}
|
Javascript
function compare(a, b) {
return a.localeCompare(b) < 0;
}
function reArrange(words) {
let indices = Array.from({ length: words.length }, (_, i) => i + 1);
indices.sort((a, b) => (compare(words[a - 1], words[b - 1]) ? -1 : 1));
let result = "" ;
for (let index of indices) {
result += index + " " ;
}
console.log(result.trim());
}
let words = [ "live" , "place" , "travel" , "word" , "sky" ];
reArrange(words);
|
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...