Given n strings, concatenate them in an order that produces the lexicographically smallest possible string.
Input : a = ["c", "cb", "cba"] Output : cbacbc Possible strings are ccbcba, ccbacb, cbccba, cbcbac, cbacbc and cbaccb. Among all these strings, cbacbc is the lexicographically smallest. Input : a = ["aa", "ab", "aaa"] Output : aaaaaab
One might think that sorting the given strings in the lexicographical order and then concatenating them produces the correct output. This approach produces the correct output for inputs like [“a”, “ab”, “abc”]. However, applying this method on [“c”, “cb”, “cba”] produces the wrong input and hence this approach is incorrect.
The correct approach is to use a regular sorting algorithm. When two strings a and b are compared to decide if they have to be swapped or not, do not check if a is lexicographically smaller than b or not. Instead check if appending b at the end of a produces a lexicographically smaller string or appending a at the end of b does. This approach works because we want the concatenated string to be lexicographically small, not the individual strings to be in the lexicographical order.
Time complexity : The above code runs inO(M * N * logN) where N is number of strings and M is maximum length of a string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Lexicographically smallest string after M operations
- Check whether given string can be generated after concatenating given strings
- Lexicographically smallest array after at-most K consecutive swaps
- Find i'th Index character in a binary string obtained after n iterations
- Largest string obtained in Dictionary order after deleting K characters
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Maximize length of the String by concatenating characters from an Array of Strings
- Longest palindromic string possible by concatenating strings from a given array
- Find the number obtained after concatenation of binary representation of M and N
- Lexicographically largest possible String after removal of K characters
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest permutation of a string with given subsequences
- Lexicographically smallest string formed by removing at most one character
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.