Участник:Alvant/TaskLastSubstringInLexicographicalOrder

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/last-substring-in-lexicographical-order

class Solution:
    def lastSubstring(self, s: str) -> str:
        symbols = list(s)
 
        last_symbol = sorted(symbols)[-1]
 
        candidate_indices = list(
            i for i in range(0, len(symbols)) if symbols[i] == last_symbol
        )
 
        last_substring = s[candidate_indices[-1]:]
 
        for candidate_index in candidate_indices[::-1]:
            candidate_substring = s[candidate_index:]
 
            if candidate_substring > last_substring:
                last_substring = candidate_substring
 
        return last_substring