Problem · String

Alphabetically Maximum Substring

Learn this problem
HardOracle logoOracleFULLTIMEPHONE SCREEN

Problem statement

Given a string s containing lowercase English letters, return its lexicographically greatest non-empty substring.

A substring is a contiguous sequence of characters from s. String a is lexicographically greater than string b when either:

  • At the first position where they differ, a has the greater character.
  • b is a proper prefix of a.

Function

alphabeticallyMaximumSubstring(s: String) → String

Examples

Example 1

s = "abab"return = "bab"

The substring bab begins with b, so it is greater than every substring beginning with a. It is also greater than the one-character substring b because b is its proper prefix.

Example 2

s = "leetcode"return = "tcode"

The only occurrence of the greatest character t begins the substring tcode, which is therefore greater than every other substring.

Example 3

s = "aaaa"return = "aaaa"

Every candidate contains only a. The full string is greatest because every shorter candidate is its proper prefix.

Constraints

  • 1 <= s.length <= 200000
  • s contains only lowercase English letters.

More Oracle problems

drafts saved locally
public String alphabeticallyMaximumSubstring(String s) {
    // write your code here
}
s"abab"
expected"bab"
checking account