Longest Nice Substring LeetCode solution in Python
1class Solution:2def longestNiceSubstring(self, s: str) -> str:3def divcon(s):4# string with length 1 or less arent considered nice5if len(s) < 2:6return ""78pivot = []9# get every character that is not nice10for i, ch in enumerate(s):11if ch.isupper() and ch.lower() not in s:12pivot.append(i)13if ch.islower() and ch.upper() not in s:14pivot.append(i)15# if no such character return the string16if not pivot:17return s18# divide the string in half excluding the char that makes the string not nice19else:20mid = (len(pivot)) // 221return max(22divcon(s[: pivot[mid]]), divcon(s[pivot[mid] + 1 :]), key=len23)2425return divcon(s)