Maximum length of consecutive 1’s in a binary string in Python using Map function
Last Updated :
27 Jul, 2022
We are given a binary string containing 1’s and 0’s. Find the maximum length of consecutive 1’s in it.
Examples:
Input : str = '110001111 01010111'
Output : 4
We have an existing solution for this problem please refer to Maximum consecutive one’s (or zeros) in a binary array link. We can solve this problem within single line of code in Python. The approach is very simple,
Separate all sub-strings of consecutive 1’s separated by zeros using split() method of string.
Print maximum length of split sub-strings of 1’s.
Implementation:
Python
def maxConsecutive1(input):
print max(map(len,input.split('0')))
if __name__ == "__main__":
input = '11000111101010111'
maxConsecutive1(input)
Time complexity : O(n) Auxiliary Space : O(1)
Share your thoughts in the comments
Add Your Comment
Please Login to comment...