프로그래밍/HackerRank
[HackerRank][Python3] Beautiful Binary String
snoopybox
2018. 9. 5. 00:16
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'))