Python3 Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String

In this problem, we will write the Python code to count the maximum sum of consecutive zeros at the start and end of the string.

The problem solution can be divided into two parts. The first part is finding all rotations of the string. The second part is finding the starting and ending consecutive zeros in all rotations of the binary string.

Another way to solve the problem is that counting the maximum consecutive zeros can answer the problem.

Problem statement – We need to find the total number of maximum consecutive zeros at the start and end of any rotation of the given binary string.

Sample examples

Input

str = "00100100"


Output

4


Explanation – Let’s take all rotations of the given string.

• 00100100 – Starting zeros – 2, Ending zeros – 2, sum – 4.

• 01001000 – Starting zeros – 1, Ending zeros – 3, sum – 4.

• 10010000 – Starting zeros – 0, Ending zeros – 4, sum – 4.

• 00100001 – Starting zeros – 2, Ending zeros – 0, sum – 2.

• 01000010 – Starting zeros – 1, Ending zeros – 1, sum – 2.

• 10000100 – Starting zeros – 0, Ending zeros – 2, sum – 2.

• 00001001 – Starting zeros – 4, Ending zeros – 0, sum – 4.

• 00010010 – Starting zeros – 3, Ending zeros – 1, sum – 4.

Input

str = ‘00000000’


Output

8


Explanation – As the string contains all zeros, the answer is equal to the string length.

Input

str = ‘111111111’


Output

0


Explanation – As the string contains all ‘1’, the answer is 0.

Approach 1

We will contact the string to itself and get substrings of length N starting from each index to get the rotational string. After that, we will calculate the sum of starting and ending zeros.

Algorithm

Step 1 – Define ‘totalOnes’ variables to store the count of ‘1’s in the given binary string.

Step 2 – Use the loop to traverse the string. If str[p] is equal to ‘1’, increase totalOnes by 1.

Step 3 – If the value of the totalOnes variable is zero, print the length of the string as the string contains only zeros.

Step 4 – Use the ‘+=’ operator to concatenate the ‘str’ string with itself and define the ‘maxZeros’ variable.

Step 5 – Traverse the concatenated string. Define the ‘startZeros’ and ‘endZeros’ variable.

Step 6 – Traverse the substring from p to p + len index. If the character at index q is not ‘0’, break the loop. Else, increase totalZeros by 1.

Step 7 – Traverse the substring from p + len -1 to p. If the character at index q is not, ‘0’ break the loop. Otherwise, increment the count of ‘endZeros’.

Step 8 – Get the sum of starting and ending zeros.

Step 9 – Use the max() method to get the maximum from sum and maxZeros.

Step 10 – Print the value of maxZeros.

Example

def countStartEndZeros(str, len):
# variable to store count of ones
totalOnes = 0
# Traverse the string
for p in range(len):
if (str[p] == '1'):
totalOnes += 1
# If the string doesn't contain any 1, print the len value
if (totalOnes == 0):
print('The maximum sum of starting and end zeros in the rotation of the string is - {}'.format(len))
return
# Merge the string
str += str
# Maximum zeros
maxZeros = 0

# Traverse the merged string
for p in range(len):
startZeros = 0
endZeros = 0
# total zeros at start
for q in range(p, p + len):
if (str[q] != '0'):
break
else:
startZeros += 1

# total zeros at the end
for q in range(p + len - 1, p - 1, -1):
if (str[q] != '0'):
break
else:
endZeros += 1
# sum of zeros at start and end
sum = startZeros + endZeros
# change maxZeros if the sum is greater
maxZeros = max(sum, maxZeros)
print('The maximum sum of starting and end zeros in the rotation of the string is - {}'.format(maxZeros))
if __name__ == "__main__":
# Given string
str = "00100100"
str_size = len(str)
countStartEndZeros(str, str_size)


Output

The maximum sum of starting and end zeros in the rotation of the string is - 4


Time complexity – O(N*N) for finding each string rotation.

Space complexity – O(N) to store concatenated string.

Approach 2

In this approach, we will solve the problem based on the observation of consecutive zeros. The answer for the problem is either maximum consecutive zeros or the sum of starting and ending zeros in the original binary string.

Algorithm

Step 1 – Print the string length if the total number of 1s in the binary string is 0.

Step 2 – Define the maxi variable to store the maximum sum of starting and ending zeros in any rotation.

Step 3 – Define the ‘zeroConsecutive’ variable to store maximum consecutive zeros in the string.

Step 4 – Traverse the string and character at pth index is ‘0’, add 1 to ‘zeroConsecutive’. Otherwise, use the max() method to get the maximum from the maxi and zeroConsecutive, and store the result to the ‘maxi’. Also, reinitialize the zeroConsecutive with zero.

Step 5 – Next, find the total number of consecutive zeros at the start and end of the string.

Step 6 – Again, update the value of the ‘maxi\’ variable if the value of ‘zeroConsecutive’ is greater.

Step 7 – Print the value of the ‘maxi’ variable.

Example

def countStartEndZeros(binStr, bin_size):
# one counts
cnt1 = 0
for p in range(bin_size):
if (binStr[p] == '1'):
cnt1 += 1
# print len if string size is equal to zero count
if (cnt1 == bin_size):
print('The maximum sum of starting and end zeros in the rotation of the string is - {}'.format(bin_size))
return
# maximum sum
maxi = 0
zeroConsecutive = 0
for p in range(bin_size):
if (binStr[p] == '0'):
zeroConsecutive += 1
else:
maxi = max(maxi, zeroConsecutive)
zeroConsecutive = 0

# Change value of maxi
maxi = max(maxi, zeroConsecutive)
# start and end zeros
left = 0
right = bin_size - 1
zeroConsecutive = 0
# lef tzeros
while (binStr[left] != '1' and left < bin_size):
zeroConsecutive += 1
left += 1
# right zeros
while (binStr[right] != '1' and right >= 0):
zeroConsecutive += 1
right -= 1
# Change value of maxi
maxi = max(maxi, zeroConsecutive)
print('The maximum sum of starting and end zeros in the rotation of the string is - {}'.format(maxi))

if __name__ == "__main__":
# Given string
str = "00100100"
str_size = len(str)
countStartEndZeros(str, str_size)



Output

The maximum sum of starting and end zeros in the rotation of the string is - 4


Time complexity – O(N) for traversing the string.

Space complexity – O(1)

Programmers should always try to reach the optimized solution to the problem. First, we can start solving the problem using the naïve approach as we have done in the first approach. After that, we can optimize it further, as we did in the second approach.

Updated on: 25-Aug-2023

47 Views