Skip to content

Instantly share code, notes, and snippets.

@banta
Last active December 9, 2016 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banta/836472b3b9aa2ec9b5a26bcebe6120f7 to your computer and use it in GitHub Desktop.
Save banta/836472b3b9aa2ec9b5a26bcebe6120f7 to your computer and use it in GitHub Desktop.
Nairuby Ruby Challenge 8th Dec, 2016

Beautiful Binary String

Alice has a binary string, B, of length n. She thinks a binary string is beautiful if and only if it doesn't contain the substring “010”.

In one step, Alice can change a 0 to a 1 (or vice-versa). Count and print the minimum number of steps needed to make Alice see the string as beautiful.

Input Format
The first line contains an integer, n (the length of binary string B).
The second line contains a single binary string, B, of length.

Constraint

  • 1 <= n <= 100
  • Each character in B E {0, 1}.

Output Format
Print the minimum number of steps needed to make the string beautiful.

Sample Input 0
7
0101010

Sample Output 0
2

Sample Input 1
5
01100

Sample Output 1
0

Sample Input 2
10
0100101010

Sample Output 2
3

# Arguments
# n => length of the binary string
# bs => binary string
def bbs(n, bs)
index = 0
step_counter = 0
while n > index
if bs[index] == '0'
# Check if the following 2 are 10
if "#{bs[index+1]}#{bs[index+2]}" == '10'
step_counter += 1
index += 3
else
index += 1
end
else
index += 1
end
end
step_counter # return number of steps
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment