Sort the Array of Strings on the basis of given substring range
Last Updated :
29 Mar, 2023
Given two positive integers I and X and an array of strings arr[], the task is to sort the given array of strings on the basis of substrings starting from index I of size X.
Examples:
Input: I = 2, X = 2, arr[] = { “baqwer”, “zacaeaz”, “aaqzzaa”, “aacaap”, “abbatyo”, “bbbacztr”, “bbbdaaa” }
Output: abbatyo bbbacztr bbbdaaa aacaap zacaeaz baqwer aaqzzaa
Explanation:
All sub-strings starting from index I = 2 and of size x = 2 are {“qw”, “ca”, “qz”, “ca”, “ba”, “ba”, “bd”}.
Sorting them in lexicographical increasing order gives {“ba”, “ba”, “bd”, “ca”, “ca”, “qw”, “qz” }, then print the corresponding original string in this order.
Input: I = 1, X = 3, arr[] = { “submit”, “source”, “skills”, “epidemic”, “ample”, “apple” }
Output: skills ample source epidemic apple submit
Approach: The idea is to create a substring of all the strings in the given array from index I of size X and keep the count of pair of a substring with the corresponding string in a map of pairs. After inserting in the map of pairs. After inserting, traverse the map and print the string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sortArray(vector<string> s,
int l, int x)
{
map<pair<string, string>, int > mp;
for ( int i = 0; i < s.size(); i++) {
string part = s[i].substr(l, x);
mp[{ part, s[i] }] += 1;
}
for ( auto it = mp.begin();
it != mp.end(); ++it) {
for ( int j = 0; j < it->second; j++) {
cout << it->first.second << ' ' ;
}
}
}
int main()
{
vector<string> arr;
arr = { "baqwer" , "zacaeaz" , "aaqzzaa" ,
"aacaap" , "abbatyo" , "bbbacztr" ,
"bbbdaaa" };
int I = 2, X = 2;
sortArray(arr, I, X);
return 0;
}
|
Java
import java.util.*;
public class Main {
static void sortArray(String[] s, int l, int x) {
Map<String, Integer> mp = new TreeMap<>();
for ( int i = 0 ; i < s.length; i++) {
String part = s[i].substring(l, l + x);
if (mp.containsKey(part + s[i])) {
mp.put(part + s[i], mp.get(part + s[i]) + 1 );
} else {
mp.put(part + s[i], 1 );
}
}
for (String key : mp.keySet()) {
for ( int j = 0 ; j < mp.get(key); j++) {
System.out.print(key.substring(x) + " " );
}
}
}
public static void main(String[] args) {
String[] arr = { "baqwer" , "zacaeaz" , "aaqzzaa" ,
"aacaap" , "abbatyo" , "bbbacztr" ,
"bbbdaaa" };
int I = 2 , X = 2 ;
sortArray(arr, I, X);
}
}
|
Python3
def sortArray(s, l, x):
mp = {}
for i in range ( len (s)):
part = s[i][l:l + x]
if (part, s[i]) in mp:
mp[(part, s[i])] + = 1
else :
mp[(part, s[i])] = 1
for key, value in sorted (mp.items()):
for j in range (value):
print (key[ 1 ], end = " " )
if __name__ = = "__main__" :
arr = [ "baqwer" , "zacaeaz" , "aaqzzaa" ,
"aacaap" , "abbatyo" , "bbbacztr" ,
"bbbdaaa" ]
I = 2
X = 2
sortArray(arr, I, X)
|
Javascript
const sortArray = (s, l, x) => {
let mp = new Map();
for (let i = 0; i < s.length; i++) {
let part = s[i].substring(l, l + x);
if (!mp.has(part + s[i])) {
mp.set(part + s[i], 1);
} else {
mp.set(part + s[i], mp.get(part + s[i]) + 1);
}
}
let sortedArray = Array.from(mp.keys()).sort();
for (let i = 0; i < sortedArray.length; i++) {
for (let j = 0; j < mp.get(sortedArray[i]); j++) {
console.log(sortedArray[i].substring(x));
}
}
};
let arr = [ "baqwer" , "zacaeaz" , "aaqzzaa" , "aacaap" , "abbatyo" , "bbbacztr" , "bbbdaaa" ,];
let I = 2,
X = 2;
sortArray(arr, I, X);
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
static void SortArray(List< string > s, int l, int x)
{
Dictionary<Tuple< string , string >, int > mp = new Dictionary<Tuple< string , string >, int >();
for ( int i = 0; i < s.Count; i++)
{
string part = s[i].Substring(l, x);
var key = new Tuple< string , string >(part, s[i]);
if (mp.ContainsKey(key))
{
mp[key]++;
}
else
{
mp[key] = 1;
}
}
foreach ( var item in mp.OrderBy(kvp => kvp.Key))
{
for ( int j = 0; j < item.Value; j++)
{
Console.Write(item.Key.Item2 + " " );
}
}
}
static void Main( string [] args)
{
List< string > arr = new List< string > {
"baqwer" ,
"zacaeaz" ,
"aaqzzaa" ,
"aacaap" ,
"abbatyo" ,
"bbbacztr" ,
"bbbdaaa"
};
int I = 2, X = 2;
SortArray(arr, I, X);
}
}
|
Output:
abbatyo bbbacztr bbbdaaa aacaap zacaeaz baqwer aaqzzaa
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...