Given a string s we have to find the lexicographical maximum substring of a string
Input : s = "ababaa" Output : babaa Explanation : "babaa" is the maximum lexicographic susbtring formed from this string Input : s = "asdfaa" Output : sdfaa
The idea is simple, we traverse through all substrings. For every substring, we compare it with current result and update result if needed.
// CPP program to find the lexicographically
// maximum substring.
using namespace std;
string LexicographicalMaxString(string str)
// loop to find the max leicographic
// substring in the substring array
string mx = "";
for (int i = 0; i < str.length(); ++i)
mx = max(mx, str.substr(i));
string str = "ababaa";
cout << LexicographicalMaxString(str);
Time Complexity : O(n)
Space Complexity : O(n)
We find largest character and all its indexes. Now we simply traverse through all instances of the largest character to find lexicographically maximum substring.
- Maximum length substring with highest frequency in a string
- Lexicographical concatenation of all substrings of a string
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Find length of longest subsequence of one string which is substring of another string
- Minimum changes required to make first string substring of second string
- Maximum length substring having all same characters after k changes
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Frequency of a substring in a string
- Check if a string is substring of another
- Shortest substring of a string containing all given words
- Searching characters and substring in a String in Java
- Convert to a string that is repetition of a substring of k length
- K-th lexicographically smallest unique substring of a given string
- Longest substring of 0s in a string formed by k concatenations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.