Skip to content

Commit

Permalink
Added maxSubstring problem
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0910 committed Jun 9, 2022
1 parent 71e6498 commit 7e484fa
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions MaxSubStringForBinaryString.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

all_examples = ["1100011","110111000","1000011", "101011"]

def maxSubArray(binaryString):
if not binaryString:
return []

maxCount = 0
count = 0
startIndex = -1
length = 0
maxLength = 0
ans= []
for i, n in enumerate(binaryString):
if n == '1':
count += 1
else:
count -= 1

length += 1

if startIndex == -1:
# starting the index again after resetting it
startIndex = i

if count > maxCount:
# highest priority of maxCount
maxCount = count
maxLength = length
ans = [binaryString[startIndex:i+1]]
elif count == maxCount and length > maxLength:
# if count matched, select the one with max length
ans = [binaryString[startIndex:i+1]]
maxLength = length
elif count == maxCount and length == maxLength:
# if count and length matches, select both
ans.append(binaryString[startIndex:i+1])

if count < 0:
# resetting the count, length and startIndex when count goes negative
count = 0
length = 0
startIndex = -1

return ans

for s in all_examples:
x = maxSubArray(s)
print(s, x)

0 comments on commit 7e484fa

Please sign in to comment.