This is the "Beautiful Binary String" problem at HackerRank:
Alice has a binary string. 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.
For example, if Alice's string is 010 she can change any one element and have a beautiful string.
This is my Python code:
def beautifulBinaryString(b):
temp = list(b)
count,i = 0,0
if len(b) == 3 and b == "010": count += 1
elif len(b) == 3 and b != "010": count = count
else:
while (i+3 <= len(temp)):
if temp[i:i+3] == ['0','1','0']:
count += 1
del temp[i:i+3]
else: i += 1
return count
It seems to me as having too many conditionals (though readable, I guess). Is there a more concise way to accomplish this?
Some test cases:
0101010
01100
2
0
This is the "Beautiful Binary String" problem at HackerRank:
Alice has a binary string. 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.
For example, if Alice's string is 010 she can change any one element and have a beautiful string.
This is my Python code:
def beautifulBinaryString(b):
temp = list(b)
count,i = 0,0
if len(b) == 3 and b == "010": count += 1
elif len(b) == 3 and b != "010": count = count
else:
while (i+3 <= len(temp)):
if temp[i:i+3] == ['0','1','0']:
count += 1
del temp[i:i+3]
else: i += 1
return count
It seems to me as having too many conditionals (though readable, I guess). Is there a more concise way to accomplish this?
Some test cases:
0101010
01100
2
0
This is the "Beautiful Binary String" problem at HackerRank:
Alice has a binary string. 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.
For example, if Alice's string is 010 she can change any one element and have a beautiful string.
This is my Python code:
def beautifulBinaryString(b):
temp = list(b)
count,i = 0,0
if len(b) == 3 and b == "010": count += 1
elif len(b) == 3 and b != "010": count = count
else:
while (i+3 <= len(temp)):
if temp[i:i+3] == ['0','1','0']:
count += 1
del temp[i:i+3]
else: i += 1
return count
It seems to me as having too many conditionals (though readable, I guess). Is there a more concise way to accomplish this?
Some test cases:
0101010
01100
2
0
- 50.1k
- 3
- 130
- 210
Make a beautiful binary string in Python
Idea: A binary stringThis is beautiful if and only if it doesn't contain the substring "010". In one step, we can change a 0 to a 1 or vice versa. Count and print the minimum number of steps needed to make us see the string as beautiful. For example, if the string is "010" we can change any one element and have a beautiful string."Beautiful Binary String " problem at HackerRank:
Alice has a binary string. 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.
For example, if Alice's string is 010 she can change any one element and have a beautiful string.
This is my Python code:
def beautifulBinaryString(b):
temp = list(b)
count,i = 0,0
if len(b) == 3 and b == "010": count += 1
elif len(b) == 3 and b != "010": count = count
else:
while (i+3 <= len(temp)):
if temp[i:i+3] == ['0','1','0']:
count += 1
del temp[i:i+3]
else: i += 1
return count
It seems to me as having too many conditionals (though readable, I guess). Is there a more concise way to accomplish this?
Some test cases:
0101010
01100
2
0
Make a beautiful binary string in Python
Idea: A binary string is beautiful if and only if it doesn't contain the substring "010". In one step, we can change a 0 to a 1 or vice versa. Count and print the minimum number of steps needed to make us see the string as beautiful. For example, if the string is "010" we can change any one element and have a beautiful string.
This is my Python code:
def beautifulBinaryString(b):
temp = list(b)
count,i = 0,0
if len(b) == 3 and b == "010": count += 1
elif len(b) == 3 and b != "010": count = count
else:
while (i+3 <= len(temp)):
if temp[i:i+3] == ['0','1','0']:
count += 1
del temp[i:i+3]
else: i += 1
return count
It seems to me as having too many conditionals (though readable, I guess). Is there a more concise way to accomplish this?
Some test cases:
0101010
01100
2
0
Make a beautiful binary string
This is the "Beautiful Binary String " problem at HackerRank:
Alice has a binary string. 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.
For example, if Alice's string is 010 she can change any one element and have a beautiful string.
This is my Python code:
def beautifulBinaryString(b):
temp = list(b)
count,i = 0,0
if len(b) == 3 and b == "010": count += 1
elif len(b) == 3 and b != "010": count = count
else:
while (i+3 <= len(temp)):
if temp[i:i+3] == ['0','1','0']:
count += 1
del temp[i:i+3]
else: i += 1
return count
It seems to me as having too many conditionals (though readable, I guess). Is there a more concise way to accomplish this?
Some test cases:
0101010
01100
2
0
Make a beautiful binary string in Python
Idea: A binary string is beautiful if and only if it doesn't contain the substring "010". In one step, we can change a 0 to a 1 or vice versa. Count and print the minimum number of steps needed to make us see the string as beautiful. For example, if the string is "010" we can change any one element and have a beautiful string.
This is my Python code:
def beautifulBinaryString(b):
temp = list(b)
count,i = 0,0
if len(b) == 3 and b == "010": count += 1
elif len(b) == 3 and b != "010": count = count
else:
while (i+3 <= len(temp)):
if temp[i:i+3] == ['0','1','0']:
count += 1
del temp[i:i+3]
else: i += 1
return count
It seems to me as having too many conditionals (though readable, I guess). Is there a more concise way to accomplish this?
Some test cases:
0101010
01100
2
0