Created
August 8, 2020 16:58
-
-
Save Daksh/4569d78a26a18a7e8a511adea9852da2 to your computer and use it in GitHub Desktop.
1542. Find Longest Awesome Substring
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
def isPalindrome(l,r): | |
diff = {} | |
for k in l.keys(): | |
diff[k] = r[k]-l[k] | |
diff = list(diff.values()) | |
countOdd = 0 | |
for e in diff: | |
if e%2==1: | |
countOdd+=1 | |
if countOdd<=1: | |
return True | |
return False | |
class Solution: | |
def longestAwesome(self, s: str) -> int: | |
d = {'0':0,'1':0,'2':0,'3':0,'4':0,'5':0,'6':0,'7':0,'8':0,'9':0} | |
prefixSum = [d.copy()] | |
for c in s: | |
d[c]+=1 | |
prefixSum.append(d.copy()) | |
maxlen = 1 | |
l = 0 | |
maxr = 0 | |
while l < len(prefixSum) - 1: | |
if maxr<=l: | |
maxr = l+1 | |
# for r in range(maxr,len(prefixSum)): | |
for r in range(len(prefixSum)-1,maxr-1,-1): | |
if isPalindrome(prefixSum[l],prefixSum[r]): | |
maxr = r | |
maxlen = max(maxlen, r-l) | |
l+=1 | |
return maxlen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment