[HackerRank][Python3] Special Palindrome Again
2018. 7. 23. 23:52 |
프로그래밍/HackerRank
Problem :
https://www.hackerrank.com/challenges/special-palindrome-again/problem
My Solution :
#!/usr/bin/env python3
def substrCount(s):
total = 0
counter = []
before = s[0]
count = 1
for c in s[1:]:
if c == before:
count += 1
else:
counter.append((before, count))
before = c
count = 1
counter.append((before, count))
for i in range(len(counter)):
count = counter[i][1]
total += count * (count + 1) // 2
if (i+2 < len(counter) and
counter[i][0] == counter[i+2][0] and
counter[i+1][1] == 1):
total += min(counter[i][1], counter[i+2][1])
return total
n = int(input())
s = input()
result = substrCount(s)
print(result)
'프로그래밍 > HackerRank' 카테고리의 다른 글
| [HackerRank][Python3] Matrix Layer Rotation (2) | 2018.08.08 |
|---|---|
| [HackerRank][Python3] Dijkstra: Shortest Reach 2 (0) | 2018.08.07 |
| [HackerRank][Python3] Tries: Contacts (0) | 2018.08.05 |
| [HackerRank][Python3] Swap Nodes [Algo] (0) | 2018.07.27 |
| [HackerRank][Python3] Fraudulent Activity Notifications (0) | 2018.07.23 |
| [HackerRank][Python3] Merge Sort: Counting Inversions (0) | 2018.07.22 |
| [HackerRank][Python3] Roads and Libraries (0) | 2018.07.22 |
| [HackerRank][Python3] BFS: Shortest Reach in a Graph (0) | 2018.07.22 |
최근에 달린 댓글 최근에 달린 댓글