[HackerRank][Python3] Two Characters
2018. 6. 11. 02:17 |
프로그래밍/HackerRank
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)
'프로그래밍 > HackerRank' 카테고리의 다른 글
| [HackerRank][Python3] New Year Chaos (0) | 2018.07.07 |
|---|---|
| [HackerRank][Python3] Minimum Swaps 2 (0) | 2018.07.06 |
| [HackerRank][Python3] Is This a Binary Search Tree? (2) | 2018.06.25 |
| [HackerRank][Python3] Candies (0) | 2018.06.14 |
| [HackerRank][Python3] Append and Delete (0) | 2018.06.10 |
| [HackerRank][Python3] Recursive Digit Sum (0) | 2018.06.07 |
| [HackerRank][Python3] Sam and substrings (0) | 2018.06.06 |
| [HackerRank][Python3] Marc's Cakewalk (0) | 2018.06.06 |
최근에 달린 댓글 최근에 달린 댓글