Sort an array of strings having characters at odd and even indices sorted in decreasing and increasing order respectively
Given an array arr[] consisting of N strings, the task is to first sort the characters at odd and even indices of the string in decreasing and increasing order respectively, for each string of the array and then, sort the modified array.
Examples:
Input: arr[] = {“ball”, “bat”, “boy”}
Output: albl atb byo
Explanation:
S[0]=”ball” is converted to “albl”
S[1]=”bat” is converted to “atb”
S[2]=”boy” is converted to “byo”
Sorted sequence of the modified array is: albl atb byo.
Input: arr[] = {“geeks”, “gfg”, “hello”, “world”}
Output: dwlro eohll esekg fgg
Approach: The given problem can be solved by traversing the given array arr[] of strings and for each string firstly sort the string and then rearrange each string such that characters at even indices are in ascending order and characters at odd indices are in descending order. In the end, sort the new array of strings in ascending order. Follow the steps below to solve the problem:
- Traverse the given array of strings arr[] and for each string perform the following steps:
- Sort the string arr[i] in alphabetical order.
- Initialize a variable, say temp as “” that stores the resultant string.
- Traverse the string arr[i] for the half-length of the string arr[i] and store the ith index from the start and the end of the string to the string temp.
- If the size of the string is odd, then add the middle character of the string arr[i] to the end of the variable temp.
- After the above steps, update the string at the ith index of the array arr[i] to the string temp.
- After completing the above steps, sort the new array of strings in ascending order and print the array of strings arr[].
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void sortString(string S[], int N)
{
for ( int i = 0; i < N; i++) {
sort(S[i].begin(), S[i].end());
int n = S[i].size();
string temp = "" ;
for ( int j = 0; j < n / 2; j++) {
temp += S[i][j];
temp += S[i][n - j - 1];
}
if (n & 1)
temp += S[i][n / 2];
S[i] = temp;
}
sort(S, S + N);
for ( int i = 0; i < N; i++) {
cout << S[i] << " " ;
}
}
int main()
{
string arr[] = { "ball" , "bat" , "boy" };
int N = sizeof (arr) / sizeof (arr[0]);
sortString(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String sortString(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static void sortString(String S[], int N)
{
for ( int i = 0 ; i < N; i++)
{
S[i] = sortString(S[i]);
int n = S[i].length();
String temp = "" ;
for ( int j = 0 ; j < n / 2 ; j++)
{
temp += S[i].charAt(j);
temp += S[i].charAt(n - j - 1 );
}
if (n % 2 == 1 )
temp += S[i].charAt(n / 2 );
S[i] = temp;
}
Arrays.sort(S);
for ( int i = 0 ; i < N; i++)
{
System.out.print(S[i] + " " );
}
}
public static void main(String[] args)
{
String arr[] = { "ball" , "bat" , "boy" };
int N = arr.length;
sortString(arr, N);
}
}
|
Python3
def sortString(S, N):
for i in range (N):
S[i] = ''.join( sorted (S[i]))
n = len (S[i])
temp = ""
for j in range (n / / 2 ):
temp + = S[i][j]
temp + = S[i][n - j - 1 ]
if (n & 1 ):
temp + = S[i][n / / 2 ]
S[i] = temp
S.sort()
for i in range (N):
print (S[i], end = " " )
if __name__ = = '__main__' :
arr = [ "ball" , "bat" , "boy" ]
N = len (arr)
sortString(arr, N)
|
C#
using System;
public class GFG{
static String sortString(String inputString)
{
char []tempArray = inputString.ToCharArray();
Array.Sort(tempArray);
return new String(tempArray);
}
static void sortString(String []S, int N)
{
for ( int i = 0; i < N; i++)
{
S[i] = sortString(S[i]);
int n = S[i].Length;
String temp = "" ;
for ( int j = 0; j < n / 2; j++)
{
temp += S[i][j];
temp += S[i][n - j - 1];
}
if (n %2== 1)
temp += S[i][n / 2];
S[i] = temp;
}
Array.Sort(S);
for ( int i = 0; i < N; i++)
{
Console.Write(S[i] + " " );
}
}
public static void Main(String[] args)
{
String []arr = { "ball" , "bat" , "boy" };
int N = arr.Length;
sortString(arr, N);
}
}
|
Javascript
<script>
function sortString(S, N)
{
for (let i = 0; i < N; i++) {
S[i] = S[i].split( "" ).sort().join( "" );
let n = S[i].length;
let temp = "" ;
for (let j = 0; j < Math.floor(n / 2); j++) {
temp += S[i][j];
temp += S[i][n - j - 1];
}
if (n & 1)
temp += S[i][(Math.floor(n / 2))];
S[i] = temp;
}
S.sort();
for (let i = 0; i < N; i++) {
document.write(S[i], " " );
}
}
let arr = [ "ball" , "bat" , "boy" ];
let N = arr.length;
sortString(arr, N);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...