Last active
July 16, 2020 03:10
-
-
Save kilfu0701/fd917471fb9a9555903af06600cf2dbe to your computer and use it in GitHub Desktop.
balanced string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(s): | |
n = len(s) | |
ans = 999 | |
for i in range(n - 1): | |
q = set([s[i]]) | |
for j in range(i + 1, n): | |
q.add(s[j]) | |
#print(i, j , q, len(q)) | |
if len(q) % 2 == 0: | |
if len(set([c.lower() for c in list(q)])) == len(q) // 2: | |
ans = min(ans, j - i + 1) | |
break | |
if ans == 999: | |
ans = -1 | |
return ans | |
print(solution('azABaabza')) # 5 | |
print(solution('TacoCat')) # -1 | |
print(solution('AcZCbaBz')) # 8 | |
print(solution('abcdefg')) # -1 | |
print(solution('aAzABaGagAgabza')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment