Quick Overview

Return the lexicographically greatest nonempty substring of a lowercase string with up to 400,000 characters. Respect standard prefix ordering without quadratic candidate generation or comparison.

Find the Lexicographically Maximum Suffix

Company: Oracle

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem Given a nonempty string, return its lexicographically greatest nonempty substring. Standard lexicographic order compares the first differing character; when one string is a prefix of another, the longer string is greater. ### Function Contract Implement `maximumSubstring(text)`. ### Constraints & Assumptions - `1 <= len(text) <= 400,000`. - `text` contains lowercase English letters. - Return the substring itself. - An `O(n^2)` list of candidate substrings is too large. ### Clarifying Questions to Ask - Are substrings contiguous? Yes. - Can the answer end before the original string ends? Extending a candidate with the remaining characters cannot make it smaller, so an optimum is always a suffix. - How are equal prefixes compared? The longer string is greater. - Is an empty answer allowed? No. ```hint Compare suffix candidates without materializing them Maintain two possible suffix starts and an offset into their current common prefix. ``` ```hint Discard a block after a mismatch When `text[i + k]` is smaller than `text[j + k]`, starts from `i` through `i + k` cannot beat the suffix at `j`. ``` ### Examples ```text text = "abab" output = "bab" text = "leetcode" output = "tcode" ``` ### Evaluation Focus - Recognizes that the answer can be restricted to suffixes. - Avoids comparing every pair of suffixes from the beginning. - Advances candidates correctly when prefixes overlap. - Runs in `O(n)` time with `O(1)` auxiliary space, excluding the returned string. ### Extensions to Discuss 1. How would a suffix array solve the same task, and at what cost? 2. How does the candidate-elimination proof handle repeated characters? 3. How would locale-aware collation change the assumptions?

Overview: Return the lexicographically greatest nonempty substring of a lowercase string with up to 400,000 characters. Respect standard prefix ordering without quadratic candidate generation or comparison.

## Problem Given a nonempty string, return its lexicographically greatest nonempty substring. Standard lexicographic order compares the first differing character; when one string is a prefix of another, the longer string is greater. ### Function Contract Implement `maximumSubstring(text)`. ### Constraints & Assumptions - `1 <= len(text) <= 400,000`. - `text` contains lowercase English letters. - Return the substring itself. - An `O(n^2)` list of candidate substrings is too large. ### Clarifying Questions to Ask - Are substrings contiguous? Yes. - Can the answer end before the original string ends? Extending a candidate with the remaining characters cannot make it smaller, so an optimum is always a suffix. - How are equal prefixes compared? The longer string is greater. - Is an empty answer allowed? No. ```hint Compare suffix candidates without materializing them Maintain two possible suffix starts and an offset into their current common prefix. ``` ```hint Discard a block after a mismatch When `text[i + k]` is smaller than `text[j + k]`, starts from `i` through `i + k` cannot beat the suffix at `j`. ``` ### Examples ```text text = "abab" output = "bab" text = "leetcode" output = "tcode" ``` ### Evaluation Focus - Recognizes that the answer can be restricted to suffixes. - Avoids comparing every pair of suffixes from the beginning. - Advances candidates correctly when prefixes overlap. - Runs in `O(n)` time with `O(1)` auxiliary space, excluding the returned string. ### Extensions to Discuss 1. How would a suffix array solve the same task, and at what cost? 2. How does the candidate-elimination proof handle repeated characters? 3. How would locale-aware collation change the assumptions?

Constraints

  • 1 <= len(text) <= 400000.
  • text contains only lowercase English letters.
  • Return the lexicographically greatest nonempty substring.
  • When one compared string is a prefix of another, the longer string is greater.

Examples

Input: ('abab',)

Expected Output: 'bab'

Explanation: Sample 1: among all suffixes, the suffix beginning at the second character is greatest.

Input: ('leetcode',)

Expected Output: 'tcode'

Explanation: Sample 2: the only suffix beginning with the greatest character t is the answer.

Hints

  1. An optimal substring can always be extended through the end of text, so compare suffix starts rather than materializing every substring.
  2. After a mismatch following an equal prefix, discard the losing candidate block and restart the comparison offset.

Loading coding console...