Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let me state the rules for ordering in a way that I think is more clear.</p> <p>String A is greater than string B if</p> <pre><code>- A is longer than B OR - A and B are the same length and A is lexicographically greater than B </code></pre> <p>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.</p> <pre><code>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) &gt; 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 &gt;= 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] &gt;= 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) } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload