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 the current result and update the 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 codeint 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 codeif __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
Optimization :
We find the largest character and all its indexes. Now we simply traverse through all instances of the largest character to find lexicographically maximum substring.
Here we follow the above approach.
C++
// C# program to find the lexicographically// maximum substring.#include <bits/stdc++.h>using namespace std;string LexicographicalMaxString(string str){ char maxchar = 'a'; vector<int> index; // We store all the indexes of maximum // characters we have in the string for (int i = 0; i < str.length(); i++) { if (str[i] >= maxchar) { maxchar = str[i]; index.push_back(i); } } string maxstring = ""; // We form a substring from that maximum // character index till end and check if // its greater that maxstring for (int i = 0; i < index.size(); i++) { if (str.substr(index[i], str.length()) > maxstring) { maxstring = str.substr(index[i], str.length()); } } return maxstring;}// Driver codeint main(){ string str = "acbacbc"; cout << LexicographicalMaxString(str); return 0;} |
Python3
# Python 3 program to find # the lexicographically# maximum substring.def LexicographicalMaxString(st): maxchar = 'a' index = [] # We store all the indexes # of maximum characters we # have in the string for i in range(len(st)): if (st[i] >= maxchar): maxchar = st[i] index.append(i) maxstring = "" # We form a substring from that # maximum character index till # end and check if its greater # that maxstring for i in range(len(index)): if (st[index[i]: len(st)] > maxstring): maxstring = st[index[i]: len(st)] return maxstring# Driver codeif __name__ == "__main__": st = "acbacbc" print(LexicographicalMaxString(st))# This code is contributed by Chitranayal |
cbc
https://youtu.be/pMS4P7wxTU8
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.
Recommended Posts:
- 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
- Minimum removals to make a string concatenation of a substring of 0s followed by a substring of 1s
- K-th lexicographical string of given length
- Print all the combinations of a string in lexicographical order
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Print all lexicographical greater permutations of a given string
- Largest lexicographical string with at most K consecutive elements
- Lexicographical concatenation of all substrings of a string
- Maximum number of times a given string needs to be concatenated to form a substring of another string
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Print all longest common sub-sequences in lexicographical order
- Sort the words in lexicographical order in Python
- Find the lexicographical next balanced bracket sequence
- Print all distinct circular strings of length M in lexicographical order
- Lexicographical smallest alternate Array
- Lexicographical ordering using Heap Sort
- Lexicographical smallest number after at most K consecutive swaps
- Generate all numbers up to N in Lexicographical Order
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.

