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 7772615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:55:38+00:00 2026-06-01T16:55:38+00:00

I need an algorithm to find the largest unique (no duplicate characters) substring from

  • 0

I need an algorithm to find the largest unique (no duplicate characters) substring from a string by removing character (no rearranging).

String A is greater than String B if it satisfies these two conditions.

1. Has more characters than String B
   Or
2. Is lexicographically greater than String B if equal length

For example, if the input string is dedede, then the possible unique combinations are de, ed, d, and e.
Of these combinations, the largest one is therefore ed since it has more characters than d and e and is lexicographically greater than de.

The algorithm must more efficient than generating all possible unique strings and sorting them to find the largest one.

Note: this is not a homework assignment.

  • 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-01T16:55:39+00:00Added an answer on June 1, 2026 at 4:55 pm

    Let me state the rules for ordering in a way that I think is more clear.

    String A is greater than string B if

    - A is longer than B
      OR
    - A and B are the same length and A is lexicographically greater than B
    

    If my restatement of the rules is correct then I believe I have a solution that runs in O(n^2) time and O(n) space. My solution is a greedy algorithm based on the observation that there are as many characters in the longest valid subsequence as there are unique characters in the input string. I wrote this in Go, and hopefully the comments are sufficient enough to describe the algorithm.

    func findIt(str string) string {
      // exc keeps track of characters that we cannot use because they have
      // already been used in an earlier part of the subsequence
      exc := make(map[byte]bool)
    
      // ret is where we will store the characters of the final solution as we
      // find them
      var ret []byte
    
      for len(str) > 0 {
        // inc keeps track of unique characters as we scan from right to left so
        // that we don't take a character until we know that we can still make the
        // longest possible subsequence.
        inc := make(map[byte]bool, len(str))
    
        fmt.Printf("-%s\n", str)
        // best is the largest character we have found that can also get us the
        // longest possible subsequence.
        var best byte
    
        // best_pos is the lowest index that we were able to find best at, we
        // always want the lowest index so that we keep as many options open to us
        // later if we take this character.
        best_pos := -1
    
        // Scan through the input string from right to left
        for i := len(str) - 1; i >= 0; i-- {
          // Ignore characters we've already used
          if _, ok := exc[str[i]]; ok { continue }
    
          if _, ok := inc[str[i]]; !ok {
            // If we haven't seen this character already then it means that we can
            // make a longer subsequence by including it, so it must be our best
            // option so far
            inc[str[i]] = true
            best = str[i]
            best_pos = i
          } else {
            // If we've already seen this character it might still be our best
            // option if it is a lexicographically larger or equal to our current
            // best.  If it is equal we want it because it is at a lower index,
            // which keeps more options open in the future.
            if str[i] >= best {
              best = str[i]
              best_pos = i
            }
          }
        }
    
    
        if best_pos == -1 {
          // If we didn't find any valid characters on this pass then we are done
          break
        } else {
          // include our best character in our solution, and exclude it for
          // consideration in any future passes.
          ret = append(ret, best)
          exc[best] = true
    
          // run the same algorithm again on the substring that is to the right of
          // best_pos
          str = str[best_pos+1:]
        }
      }
      return string(ret)
    }
    

    I am fairly certain you can do this in O(n) time, but I wasn’t sure of my solution so I posted this one instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need an algorithm to find all of the subsets of a set where
Possible Duplicate: Algorithm to find minimum number of weighings required to find defective ball
For some game where one would need to find anagrams from a bunch of
I need an algorithm to find out all the possible positions of a group
I need to find the smallest power of two that's greater or equal to
Given a directed graph, I need to find the minimum set of vertices from
I need to find an algorithm which determines a relationship between a square and
I need to create an algorithm that could find all the critical paths in
I need help in understanding this interview question: Q: Find an algorithm to find
I have an array of integers, and I need an O(n) algorithm to find

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.