1763. 最长的美好子字符串
为保证权益,题目请参考 1763. 最长的美好子字符串(From LeetCode).
解决方案1
Python
python
# 1763. 最长的美好子字符串
# https://leetcode-cn.com/problems/longest-nice-substring/
class Solution:
def longestNiceSubstring(self, s: str) -> str:
maxLen = 0
maxStart = 0
for left in range(len(s)):
se = set()
for right in range(left, len(s)):
se.add(s[right])
isOk = True
for k in se:
if not ((k.islower() and k.upper() in se ) or (k.isupper() and k.lower() in se)):
isOk = False
break
if isOk:
if right - left + 1 > maxLen:
maxLen = right - left + 1
maxStart = left
return s[maxStart:maxStart+maxLen]
if __name__ == "__main__":
solution = Solution()
print(solution.longestNiceSubstring("YazaAay"))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30