[HackerRank][Python3] Beautiful Binary String
2018. 9. 5. 00:16 |
프로그래밍/HackerRank
Problem :
https://www.hackerrank.com/challenges/beautiful-binary-string/problem
My Solution :
#!/usr/bin/env python3
def beautiful_binary_string(b):
i = count = 0
while i < len(b)-2:
if b[i:i+3] == '010':
count += 1
i += 3
else:
i += 1
return count
n = int(input())
b = input()
result = beautiful_binary_string(b)
print(result)
Comment :
내가 처음 문제를 접한 후 머리 속에 바로 떠올린 알고리즘이 위 풀이이다. 그런데 곰곰히 생각해보니 위 알고리즘은 그냥 string에서 '010'의 갯수를 세는 것과 같다. 따라서 한 줄로 표현하면 아래와 같다.
#!/usr/bin/env python3
input(); print(input().count('010'))
'프로그래밍 > HackerRank' 카테고리의 다른 글
| [HackerRank][Python3] Equalize the Array (0) | 2018.09.06 |
|---|---|
| [HackerRank][Python3] Jumping on the Clouds (0) | 2018.09.06 |
| [HackerRank][Python3] Sequence Equation (0) | 2018.09.05 |
| [HackerRank][Python3] String Construction (0) | 2018.09.05 |
| [HackerRank][Python3] Separate the Numbers (0) | 2018.09.04 |
| [HackerRank][Python3] Weighted Uniform Strings (0) | 2018.09.04 |
| [HackerRank][Python3] HackerRank in a String! (0) | 2018.09.03 |
| [HackerRank][Python3] Truck Tour (0) | 2018.09.03 |
최근에 달린 댓글 최근에 달린 댓글