1133. Last Substring in Lexicographical Order
My accepted Python solution to LeetCode problem 1133, Last Substring in Lexicographical Order, running in 156ms.
- Difficulty: Hard
- Python
- Runtime 156ms
- Memory 21MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 156ms, memory 21MB, accepted 2026-01-02.
class Solution:
def lastSubstring(self, s: str) -> str:
# Two pointer approach
n = len(s)
i, j, k = 0, 1, 0
while j + k < n:
if s[i + k] == s[j + k]:
k += 1
elif s[i + k] < s[j + k]:
i = max(i + k + 1, j)
j = i + 1
k = 0
else:
j = j + k + 1
k = 0
return s[i:]