Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8232597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:53:11+00:00 2026-06-07T17:53:11+00:00

The question is to generate the lexicographically greatest string given some string s. So

  • 0

The question is to generate the lexicographically greatest string given some string s.
So the aim is to find lexicographically greatest, unique(no repetitions) substring s1 from s.
We say that some subsequence s1 is greater than another subsequence s2 if s1 has more characters than s2 or s1 is lexicographically greater than s2 if equal length.

I/O are as follows:

Input is: babab

output is: ba

Second input is: nlhthgrfdnnlprjtecpdrthigjoqdejsfkasoctjijaoebqlrgaiakfsbljmpibkidjsrtkgrdnqsknbarpabgokbsrfhmeklrle

Second output is:
tsocrpkijgdqnbafhmle

This is what I wrote for my java code but my code fails on the second test case. Also I’m having a hard time understanding why second output isn’t tsrqponmlkjihgfedcba.
Can somebody provide suggestions for a fix or even java code?

I think the algorithm has to be more efficient than generating all possible unique strings, sort them and find lexicographically largest one.

To make the question much clearer, if the input is babab, then all the possible unique combinations would be b, a, ba, ab. And the output will be ba because it’s the longest and lexicographically greater than ab.

Note: this is not a homework assignment.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class mostBeautiful {
final static int MAX = 1000000;
static String[] permute;
static void permutation(String prefix, String str, int counter) {
    int n = str.length();
    //System.out.println("n is: "+ n);
    if (n == 0) {
        permute[counter] = prefix;
    } else {
        for (int i = 0; i < n; i++) {
            //System.out.println("str is: "+ str);
            permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n), counter++);
        }
    }
}
public static void main(String[] args) throws IOException {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String s = bf.readLine();
    char[] unique = new char[26];
    int counter = 0;
    String answer = "";
    //System.out.println("s is: " + s);
    int ascii = 0;
    final int asciiAVal = 97;
    final int asciiZVal = 122;
    for (int i = 0; i < s.length(); i++) {
        ascii = (int)s.charAt(i);
        if (ascii < asciiAVal || ascii > asciiZVal) {
            continue;
        }
        char ch = s.charAt(i);
        unique[ch - 'a'] = ch;
    }
    String result = "";
    for (int j = 25; j >= 0; j--) {
        result += unique[j];
    }
    result = result.trim();
    System.out.println(result);
    int size = result.length() * (result.length() - 1);
    permute = new String[size];
    permutation("", result, counter);
    for (int i = 1; i < size; i++) {
        if (permute[i].compareTo(permute[i - 1]) > 0){
            answer = permute[i];
        }  else {
            answer = permute[i - 1];
        }

    }
    System.out.println("answer is: " + answer);
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:53:12+00:00Added an answer on June 7, 2026 at 5:53 pm

    After thinking about this problem in many ways, I have determined a divide-and-conquer algorithm that gets the results right:

    Algorithm – Pseudocode

    Assuming some input string, S defined as a concatenation of two substrings A + B, we compute the lexicographically greatest string recursively as:

    LexMax(S) = Merge(LexMax(A),LexMax(B))
    

    Where

    LexMax(S)
    {
        if Length(S) = 1
            return S
        else
        {
            LMA = LexMax(S[0:Length/2])
            LMB = LexMax(S[Length/2:end])
            return Merge(LMA,LMB)
        }
    }
    
    Merge(A,B)
    {
        Sa = A
        Sb = B
    
        for n = 0:Length(A)
        {
            if Sb contains A[n]
            {
                if A[n+1:end] contains character > A[n]
                    Remove A[n] from Sa
                else
                    Remove A[n] from Sb
            }
        }
    
        return Sa + Sb
    }
    

    Java Code

    Coming soon!

    Example

    Given an input string

    cefcfdabbcfed
    

    Divide it into

    cefcfda
    bbcfed
    

    Assuming the function works we have:

    LexMax("cefcfda") = "efcda"
    LexMax("bbcfed") = "bcfed"
    

    Merging works as follows:

    e: efcda bcfed
    

    In both substrings, greater value found to right of e in left substring, remove from left

    f: fcda bcfed
    

    In both substrings, no greater value in left substring, remove from right

    c: fcda bced
    

    In both substrings, greater value found to right of c in left substring, remove from left

    d: fda bced
    

    In both substrings, no greater value in left substring, remove from right

    a: fda bce
    

    Not in both substrings, do nothing

    Final result:

    LexMax(cefcfdabbcfed) = fdabce
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

really basic question, I have a string to generate as SQL statement that I
Hello Stacked-Experts! My question: How to generate a string from a CLLocationDegrees value? Failed
This follows on from this question: Algorithm to generate spanning set Given this input:
I asked this question before: Generate XML from a class I want to do
Like my question, i need to generate random numbers that have identical pairs between
I have a question with php. I wanna generate a signature based on some
I want to generate Javascript code from an existing Java project ( original question
Question: Can CMake generate build scripts that do not, in any way, use CMake?
I have a question to the following procedure: script/generate scaffold product title:string description:text db:migrate
Follows on from this question Algorithm to generate (not quite) spanning set in Python

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.