Lexicographical Maximum substring of string
Given a string s we have to find the lexicographical maximum substring of a string
Examples:
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.
Below is the implementation:
C++
// CPP program to find the lexicographically // maximum substring. #include <bits/stdc++.h> 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)); return mx; } // Driver code int main() { string str = "ababaa"; cout << LexicographicalMaxString(str); return 0; } |
Java
//Java program to find the lexicographically // maximum substring. class GFG { static 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) { if (mx.compareTo(str.substring(i)) <= 0) { mx = str.substring(i); } } return mx; } // Driver code public static void main(String[] args) { String str = "ababaa"; System.out.println(LexicographicalMaxString(str)); } } // This code is contributed by 29AjayKumar |
Python3
# Python 3 program to find the # lexicographically maximum substring. def LexicographicalMaxString(str): # loop to find the max leicographic # substring in the substring array mx = "" for i in range(len(str)): mx = max(mx, str[i:]) return mx # Driver code if __name__ == '__main__': str = "ababaa" print(LexicographicalMaxString(str)) # This code is contributed by # Sanjit_Prasad |
C#
// C# program to find the lexicographically // maximum substring. using System; public class GFG{ static 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) { if (mx.CompareTo(str.Substring(i)) <= 0) { mx = str.Substring(i); } } return mx; } // Driver code public static void Main() { String str = "ababaa"; Console.WriteLine(LexicographicalMaxString(str)); } } // This code is contributed by 29AjayKumar |
babaa
Time Complexity : O(n)
Space Complexity : O(n)
Optimization :
We find largest character and all its indexes. Now we simply traverse through all instances of the largest character to find lexicographically maximum substring.
Recommended Posts:
- Maximum length substring with highest frequency in a string
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Lexicographical concatenation of all substrings of a string
- Print all the combinations of a string in lexicographical order
- 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
- Queries to check if string B exists as substring in string A
- Maximum length substring having all same characters after k changes
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Maximum length palindromic substring such that it starts and ends with given char
- Frequency of a substring in a string
- Check if a string is substring of another
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Frequency of a substring in a string using pthread
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.


