Longest Nice Substring LeetCode solution in Python

1
class Solution:
2
def longestNiceSubstring(self, s: str) -> str:
3
def divcon(s):
4
# string with length 1 or less arent considered nice
5
if len(s) < 2:
6
return ""
7

8
pivot = []
9
# get every character that is not nice
10
for i, ch in enumerate(s):
11
if ch.isupper() and ch.lower() not in s:
12
pivot.append(i)
13
if ch.islower() and ch.upper() not in s:
14
pivot.append(i)
15
# if no such character return the string
16
if not pivot:
17
return s
18
# divide the string in half excluding the char that makes the string not nice
19
else:
20
mid = (len(pivot)) // 2
21
return max(
22
divcon(s[: pivot[mid]]), divcon(s[pivot[mid] + 1 :]), key=len
23
)
24

25
return divcon(s)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0