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
1. 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.
2. Sort the string array to make them all in lexicographical order.
3. Concatenate the strings of string array in another empty string.
C++
// CPP Program to create concatenation of all // substrings in lexicographic order. #include <bits/stdc++.h> using namespace std; string lexicographicSubConcat(string s) { int n = s.length(); // Creating an array to store substrings int sub_count = n*(n+1)/2; string arr[sub_count]; // finding all substrings of string 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 all substrings in lexicographic // order sort(arr, arr + sub_count); // Concatenating all substrings 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
// Java Program to create concatenation of all // substrings in lexicographic order. import java.util.*; class GFG { static String lexicographicSubConcat(String s) { int n = s.length(); // Creating an array to store substrings int sub_count = n*(n+1)/2; String []arr = new String[sub_count]; // finding all substrings of string 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); } // Sort all substrings in lexicographic // order Arrays.sort(arr); // Concatenating all substrings String res = ""; for (int i = 0; i < sub_count; i++) res += arr[i]; return res; } // Driver code public static void main(String[] args) { String s = "abc"; System.out.println(lexicographicSubConcat(s)); } } // This code has been contributed by 29AjayKumar |
Python3
# Python Program to create concatenation of all # substrings in lexicographic order. def lexicographicSubConcat(s): n = len(s); # Creating an array to store substrings sub_count = (n * (n + 1))//2; arr = [0]*sub_count; # finding all substrings of string index = 0; for i in range(n): for j in range(1,n - i + 1): arr[index] = s[i:i + j]; index += 1; # Sort all substrings in lexicographic # order arr.sort(); # Concatenating all substrings res = ""; for i in range(sub_count): res += arr[i]; return res; s = "abc"; print(lexicographicSubConcat(s)); # This code is contributed by Princi Singh |
C#
// C# Program to create concatenation of all // substrings in lexicographic order. using System; class GFG { static String lexicographicSubConcat(String s) { int n = s.Length; // Creating an array to store substrings int sub_count = n*(n+1)/2; String []arr = new String[sub_count]; // finding all substrings of string int index = 0; for (int i = 0; i < n; i++) for (int len = 1; len <= n - i; len++) { arr[index++] = s.Substring(i, len); } // Sort all substrings in lexicographic // order Array.Sort(arr); // Concatenating all substrings String res = ""; for (int i = 0; i < sub_count; i++) res += arr[i]; return res; } // Driver code public static void Main(String[] args) { String s = "abc"; Console.WriteLine(lexicographicSubConcat(s)); } } /* This code contributed by PrinciRaj1992 */ |
Output:
aababcbbcc
Recommended Posts:
- Lexicographical Maximum substring of string
- Print all the combinations of a string in lexicographical order
- Print Concatenation of Zig-Zag String in 'n' Rows
- Ropes Data Structure (Fast String Concatenation)
- Replace two substrings (of a string) with each other
- Number of substrings of a string
- Split the string into substrings using delimiter
- Program to print all substrings of a given string
- Count of substrings of a binary string containing K ones
- Number of even substrings in a string of digits
- Sum of all substrings of a string representing a number | Set 1
- Number of substrings of one string present in other
- Different substrings in a string that start and end with given strings
- Number of substrings divisible by 6 in a string of integers
- Number of substrings with odd decimal value in a binary string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



