FastPrepLexicographically Largest Distinct-Letter Subsequence

Lexicographically Largest Distinct-Letter Subsequence

Navan logoNavan● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a lowercase string s, remove characters so that every distinct letter appears exactly once. The remaining characters must preserve their original relative order.

Return the lexicographically largest possible result.

Function

largestUniqueLetters(s: String) → String

Examples

Example 1

s = "bcabc"return = "cab"

The subsequence cab contains each distinct letter once and is lexicographically largest.

Example 2

s = "cbacdcbc"return = "cbad"

Keeping the early c and b allows the largest valid prefix.

Example 3

s = "bbcaac"return = "bca"

The best unique-letter subsequence is bca.

Constraints

  • 1 ≤ s.length ≤ 100000.
  • s contains only lowercase English letters.

More Navan problems

See Navan hiring insights
public String largestUniqueLetters(String s) {
    // write your code here
}
s"bcabc"
expected"cab"
Checking account…