Lexicographical concatenation of all substrings of a string | Javascript Program to create concatenation of all substrings in lexicographic order . ; Creating an array to store substrings ; finding all substrings of string ; Sort all substrings in lexicographic order ; Concatenating all substrings ; Driver code
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 = " " ; document . write ( lexicographicSubConcat ( s ) ) ;