1763. 最长的美好子字符串

exiaohu 于 2022-02-01 发布

题目链接:1763. 最长的美好子字符串

从最长的字符串开始,一一判断即可。

import itertools


class Solution:
    @staticmethod
    def is_nice_substring(s: str) -> bool:
        return len(set(s)) == len(set(s.lower())) * 2

    def longestNiceSubstring(self, s: str) -> str:
        for i, j in sorted(itertools.combinations(range(len(s)), 2), key=lambda item: (item[0] - item[1], item[0])):
            if self.is_nice_substring(s[i:j + 1]):
                return s[i:j + 1]
        return ''