Created
December 10, 2019 02:31
-
-
Save liketheflower/00455da83382a5a8b81e8ab5b6d36226 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Solution: | |
def threeEqualParts(self, A: List[int]) -> List[int]: | |
S = ''.join([str(a) for a in A]) | |
cnt_1 = S.count('1') | |
if cnt_1 == 0:return [0, 2] | |
# each part should have the same number of '1' | |
if cnt_1 % 3!=0:return [-1, -1] | |
counter_1, idx, first = 0, 0, '' | |
leading_zero = True | |
for i, s in enumerate(S): | |
if s=='1': | |
leading_zero = False | |
counter_1 += 1 | |
if not leading_zero: | |
first += s | |
if counter_1==cnt_1 //3: | |
idx = i | |
break | |
end_1 = S.rfind('1') | |
# Find pending_zero, the final result will be first+pending_zero | |
# verify whether each part can match the final result. | |
pending_zero = len(S)-1-end_1 | |
if S[:-pending_zero if pending_zero else len(S)].endswith(first): | |
# if contains enough zero for first part to match the pending zero of last part. | |
if pending_zero==0 or S[idx+1:].startswith(pending_zero*'0'): | |
# verify 1) whether we can find first in the middle | |
#2) whether we have enough zero for second part to match the pending zero of last part | |
temp_str = S[idx+1+pending_zero: -(pending_zero+len(first))] | |
idx1 = temp_str.find(first) | |
if idx1 != -1 and (pending_zero==0 or temp_str.endswith('0'*pending_zero)): | |
ii = idx+pending_zero | |
return [ii, ii+1+idx1+len(first)+pending_zero ] | |
return [-1, -1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment