DEV Community

Abhishek Chaudhary
Abhishek Chaudhary

Posted on

Last Substring in Lexicographical Order

Given a string s, return the last substring of s in lexicographical order.

Example 1:

Input: s = "abab"
Output: "bab"
Explanation: The substrings are ["a", "ab", "aba", "abab", "b", "ba", "bab"]. The lexicographically maximum substring is "bab".

Example 2:

Input: s = "leetcode"
Output: "tcode"


  • 1 <= s.length <= 4 * 105
  • s contains only lowercase English letters.


class Solution:
    def lastSubstring(self, s: str) -> str:
        n = len(s)
        maxSub = ""
        for i in range(n):
            maxSub = max(maxSub, s[i:])
        return maxSub
Enter fullscreen mode Exit fullscreen mode

Top comments (0)