FastPrepLexicographically Last Substring

Lexicographically Last Substring

Roblox logoRoblox● HardNEW GRADOA
Learn

Problem statement

Consider every distinct nonempty contiguous substring of s, sorted in ascending lexicographic order. Return the last string in that order.

For this exercise, assume s contains lowercase English letters. Compare strings at their first differing letter; if one is a prefix of the other, the longer string is larger. Return an empty string when s is empty.

You only need to return the selected substring, not construct or return the full collection of substrings.

Function

lastSubstring(s: String) → String

Examples

Example 1

s = "abab"return = "bab"

Among the substrings starting with b, bab is larger than b. Every substring starting with a is smaller.

Example 2

s = "leetcode"return = "tcode"

t is the largest available first letter, and its full suffix is the largest substring starting there.

Example 3

s = "aaaa"return = "aaaa"

When strings share a prefix, the longer one is larger.

Constraints

  • 0 <= s.length <= 5000.
  • Every character is a lowercase English letter.

More Roblox problems

See Roblox hiring insights
public String lastSubstring(String s) {
    // write your code here
}
s"abab"
expected"bab"
Checking account…