Lexicographical concatenation of all substrings of a string
Given a string, find concatenation of all substrings in lexicographic order.
Examples:
Input : s = “abc”
Output : aababcbbcc
The substrings of s in lexicographic order are “a”, “b”, “c”, “ab”, “abc”, “bc”. Concatenation of substrings is “a”+”ab”+”abc”+”b”+”bc”+”c” = “aababcbbcc”.
Input : s = “cba”
Output : abbaccbcba
- Find all the substrings of string and store it in a string array. The size of array would be n*(n+1)/2 where n is length of input string.
- Sort the string array to make them all in lexicographical order.
- Concatenate the strings of string array in another empty string.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
string lexicographicSubConcat(string s)
{
int n = s.length();
int sub_count = n*(n+1)/2;
string arr[sub_count];
int index = 0;
for ( int i = 0; i < n; i++)
for ( int len = 1; len <= n - i; len++)
arr[index++] = s.substr(i, len);
sort(arr, arr + sub_count);
string res = "" ;
for ( int i = 0; i < sub_count; i++)
res += arr[i];
return res;
}
int main()
{
string s = "abc" ;
cout << lexicographicSubConcat(s);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String lexicographicSubConcat(String s)
{
int n = s.length();
int sub_count = n*(n+ 1 )/ 2 ;
String []arr = new String[sub_count];
int index = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int len = 1 ; len <= n - i; len++)
{
arr[index++] = s.substring(i, i+len);
}
Arrays.sort(arr);
String res = "" ;
for ( int i = 0 ; i < sub_count; i++)
res += arr[i];
return res;
}
public static void main(String[] args)
{
String s = "abc" ;
System.out.println(lexicographicSubConcat(s));
}
}
|
Python3
def lexicographicSubConcat(s):
n = len (s);
sub_count = (n * (n + 1 )) / / 2 ;
arr = [ 0 ] * sub_count;
index = 0 ;
for i in range (n):
for j in range ( 1 ,n - i + 1 ):
arr[index] = s[i:i + j];
index + = 1 ;
arr.sort();
res = "";
for i in range (sub_count):
res + = arr[i];
return res;
s = "abc" ;
print (lexicographicSubConcat(s));
|
C#
using System;
class GFG
{
static String lexicographicSubConcat(String s)
{
int n = s.Length;
int sub_count = n*(n+1)/2;
String []arr = new String[sub_count];
int index = 0;
for ( int i = 0; i < n; i++)
for ( int len = 1; len <= n - i; len++)
{
arr[index++] = s.Substring(i, len);
}
Array.Sort(arr);
String res = "" ;
for ( int i = 0; i < sub_count; i++)
res += arr[i];
return res;
}
public static void Main(String[] args)
{
String s = "abc" ;
Console.WriteLine(lexicographicSubConcat(s));
}
}
|
Javascript
<script>
function lexicographicSubConcat(s)
{
var n = s.length;
var sub_count = n*parseInt((n+1)/2);
var arr = Array(sub_count);
var index = 0;
for ( var i = 0; i < n; i++)
for ( var len = 1; len <= n - i; len++)
arr[index++] = s.substring(i,i+ len);
arr.sort();
var res = "" ;
for ( var i = 0; i < sub_count; i++)
res += arr[i];
return res;
}
var s = "abc" ;
document.write( lexicographicSubConcat(s));
</script>
|
Time Complexity: O(n3)
Auxiliary Space: O(n)
Last Updated :
01 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...