Lexicographical concatenation of all substrings of a string - GeeksforGeeks

28 May, 2021
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++
Java
Python3
C#
Javascript
// 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 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 codepublic static void main(String[] args){    String s = "abc";    System.out.println(lexicographicSubConcat(s));}} // This code has been contributed by 29AjayKumar
# 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# 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 codepublic static void Main(String[] args){    String s = "abc";    Console.WriteLine(lexicographicSubConcat(s));}} /* This code contributed by PrinciRaj1992 */
<script> // Javascript Program to create concatenation of all// substrings in lexicographic order. function lexicographicSubConcat(s){    var n = s.length;     // Creating an array to store substrings    var sub_count = n*parseInt((n+1)/2);    var arr = Array(sub_count);         // finding all substrings of string    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);         // Sort all substrings in lexicographic    // order    arr.sort();     // Concatenating all substrings    var res = "";    for (var i = 0; i < sub_count; i++)        res += arr[i];                 return res;   } var s = "abc";document.write( lexicographicSubConcat(s)); </script>
Output:  
aababcbbcc
 
YouTubeGeeksforGeeks508K subscribersLexicographical concatenation of all substrings of a string | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:03•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=iUkj6mKrKSw" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
29AjayKumar
princiraj1992
princi singh
rrrtnx
lexicographic-ordering
Sorting
Strings
Strings
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Chocolate Distribution Problem
C++ Program for QuickSort
Stability in sorting algorithms
Quick Sort vs Merge Sort
Quickselect Algorithm
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Longest Common Subsequence | DP-4