Finding longest substring in alphabetical order

后端 未结 17 1517
刺人心
刺人心 2020-12-06 15:15

EDIT: I am aware that a question with similar task was already asked in SO but I\'m interested to find out the problem in this specific piece of code. I am

17条回答
  •  甜味超标
    2020-12-06 15:47

    I came up with this solution

    def longest_sorted_string(s):
        max_string = ''
        for i in range(len(s)):
            for j in range(i+1, len(s)+1):
                string = s[i:j]
                arr = list(string)
                if sorted(string) == arr and len(max_string) < len(string):
                    max_string = string
        return max_string
    

提交回复
热议问题