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

&nbsp;
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"


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 4 * 105
	s contains only lowercase English letters.


class Solution:
    def lastSubstring(self, s: str) -> str:
        i = 0
        j = 1
        k = 0
        n = len(s)
        while j + k < n:
            if s[i + k] == s[j + k]:
                k += 1
            elif s[i + k] > s[j + k]:
                j += k + 1
                k = 0
            elif s[i + k] < s[j + k]:
                i = max(i + k + 1, j)
                j = i + 1
                k = 0
        return s[i:]
class Solution {

public String lastSubstring(String s) {
int maxIndex = s.length() - 1;

for(int currIndex = s.length() - 1 ; currIndex >= 0 ; currIndex--){
    if(s.charAt(currIndex) > s.charAt(maxIndex))
        maxIndex = currIndex;

    else if(s.charAt(currIndex) == s.charAt(maxIndex)){
        int i = currIndex + 1;
        int j = maxIndex + 1;

        while(i < maxIndex && j < s.length() && s.charAt(i) == s.charAt(j)){
            i++;
            j++;
        }

        if(i == maxIndex || j == s.length() || s.charAt(i) > s.charAt(j))
            maxIndex = currIndex;
    }
}

return s.substring(maxIndex);
}
}
class Solution
{
    public:
        string lastSubstring(string s)
        {
            int maxIndex = s.length() - 1;

            for (int currIndex = s.length() - 1; currIndex >= 0; currIndex--)
            {
                if (s[currIndex] > s[maxIndex])
                    maxIndex = currIndex;

                else if (s[currIndex] == s[maxIndex])
                {
                    int i = currIndex + 1;
                    int j = maxIndex + 1;

                    while (i < maxIndex && j < s.length() && s[i] == s[j])
                    {
                        i++;
                        j++;
                    }

                    if (i == maxIndex || j == s.length() || s[i] > s[j])
                        maxIndex = currIndex;
                }
            }

            return s.substr(maxIndex);
        }
};
var lastSubstring = function(s) {
    let res = 0;
    for(let i = 1; i < s.length; i++){
        if(s.slice(i) > s.slice(res)) res = i;
    };
    return s.slice(res);
};
Last Substring in Lexicographical Order