프로그래밍/HackerRank
[HackerRank][Python3] Two Characters
snoopybox
2018. 6. 11. 02:17
Problem :
https://www.hackerrank.com/challenges/two-characters/problem
My Solution :
#!/usr/bin/env python3
def is_alternating(a, b, s):
t = 0
for c in s:
if c in (a, b):
if c == t:
return False
t = c
return True
def twoCharaters(s):
cnt_c = {}
for c in s:
cnt_c.setdefault(c, 0)
cnt_c[c] += 1
sorted_keys = sorted(cnt_c, key=cnt_c.get, reverse=True)
for i in range(len(sorted_keys)-1):
for j in range(i+1, len(sorted_keys)):
if cnt_c[sorted_keys[i]] - cnt_c[sorted_keys[j]] > 1:
break
if is_alternating(sorted_keys[i], sorted_keys[j], s):
return cnt_c[sorted_keys[i]] + cnt_c[sorted_keys[j]]
return 0
l = int(input())
s = input().strip()
result = twoCharaters(s)
print(result)