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 substring 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++
#include <bits/stdc++.h>
using namespace std;
string LexicographicalMaxString(string str)
{
string mx = "" ;
for ( int i = 0; i < str.length(); ++i)
mx = max(mx, str.substr(i));
return mx;
}
int main()
{
string str = "ababaa" ;
cout << LexicographicalMaxString(str);
return 0;
}
|
Java
class GFG {
static String LexicographicalMaxString(String str)
{
String mx = "" ;
for ( int i = 0 ; i < str.length(); ++i) {
if (mx.compareTo(str.substring(i)) <= 0 ) {
mx = str.substring(i);
}
}
return mx;
}
public static void main(String[] args)
{
String str = "ababaa" ;
System.out.println(LexicographicalMaxString(str));
}
}
|
Python3
def LexicographicalMaxString( str ):
mx = ""
for i in range ( len ( str )):
mx = max (mx, str [i:])
return mx
if __name__ = = '__main__' :
str = "ababaa"
print (LexicographicalMaxString( str ))
|
C#
using System;
public class GFG {
static String LexicographicalMaxString(String str)
{
String mx = "" ;
for ( int i = 0; i < str.Length; ++i) {
if (mx.CompareTo(str.Substring(i)) <= 0) {
mx = str.Substring(i);
}
}
return mx;
}
public static void Main()
{
String str = "ababaa" ;
Console.WriteLine(LexicographicalMaxString(str));
}
}
|
Javascript
<script>
function LexicographicalMaxString(str)
{
var mx = "" ;
for ( var i = 0; i < str.length; ++i) {
if (mx.localeCompare(str.substring(i)) <= 0) {
mx = str.substring(i);
}
}
return mx;
}
var str = "ababaa" ;
document.write(LexicographicalMaxString(str));
</script>
|
Complexity Analysis:
- Time complexity : O(n2)
- Auxiliary Space : O(n)
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++
#include <bits/stdc++.h>
using namespace std;
string LexicographicalMaxString(string str)
{
char maxchar = 'a' ;
vector< int > index;
for ( int i = 0; i < str.length(); i++) {
if (str[i] >= maxchar) {
maxchar = str[i];
index.push_back(i);
}
}
string 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;
}
int main()
{
string str = "acbacbc" ;
cout << LexicographicalMaxString(str);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static String LexicographicalMaxString(String str)
{
char maxchar = 'a' ;
ArrayList<Integer> index = new ArrayList<Integer>();
for ( int i = 0 ; i < str.length(); i++)
{
if (str.charAt(i) >= maxchar)
{
maxchar = str.charAt(i);
index.add(i);
}
}
String maxstring = "" ;
for ( int i = 0 ; i < index.size(); i++)
{
if (str.substring(index.get(i),
str.length()).compareTo( maxstring) > 0 )
{
maxstring = str.substring(index.get(i),
str.length());
}
}
return maxstring;
}
public static void main (String[] args)
{
String str = "acbacbc" ;
System.out.println(LexicographicalMaxString(str));
}
}
|
Python3
def LexicographicalMaxString(st):
maxchar = 'a'
index = []
for i in range ( len (st)):
if (st[i] > = maxchar):
maxchar = st[i]
index.append(i)
maxstring = ""
for i in range ( len (index)):
if (st[index[i]: len (st)] >
maxstring):
maxstring = st[index[i]:
len (st)]
return maxstring
if __name__ = = "__main__" :
st = "acbacbc"
print (LexicographicalMaxString(st))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static string LexicographicalMaxString( string str)
{
char maxchar = 'a' ;
List< int > index = new List< int >();
for ( int i = 0; i < str.Length; i++)
{
if (str[i] >= maxchar)
{
maxchar = str[i];
index.Add(i);
}
}
string maxstring = "" ;
for ( int i = 0; i < index.Count; i++)
{
if (str.Substring(index[i]).CompareTo(maxstring) > 0)
{
maxstring = str.Substring(index[i]);
}
}
return maxstring;
}
static public void Main()
{
string str = "acbacbc" ;
Console.Write(LexicographicalMaxString(str));
}
}
|
Javascript
<script>
function LexicographicalMaxString(str)
{
let maxchar = 'a' ;
let index = [];
for (let i = 0; i < str.length; i++)
{
if (str[i] >= maxchar)
{
maxchar = str[i];
index.push(i);
}
}
let maxstring = "" ;
for (let i = 0; i < index.length; i++)
{
if (str.substring(index[i],
str.length) > maxstring)
{
maxstring = str.substring(index[i],
str.length);
}
}
return maxstring;
}
let str = "acbacbc" ;
document.write(LexicographicalMaxString(str));
</script>
|
Complexity Analysis:
- Time Complexity: O(n*m) where n is the length of the string and m is the size of index array.
- Auxiliary Space: O(n + m) where n is the length of the string and m is the size of index array.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Aug, 2022
Like Article
Save Article