参考:

https://leetcode-cn.com/problems/longest-nice-substring/solution/zui-chang-de-mei-hao-zi-zi-fu-chuan-by-l-4l1t/

import collections

def check(s):
    d = collections.Counter(list(s))
    keys = d.keys()
    for i in keys:
        if i.lower() in keys and i.upper() in keys:
            continue
        else:
            return False
    return True
class Solution:
    def longestNiceSubstring(self, s: str) -> str:
        m = ""
        for i in range(len(s)):
            for j in range(i, len(s)+1):
                if check(s[i:j]):
                    if len(s[i:j]) > len(m):
                        m = s[i:j]
        return m