-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |