**題目:**給定一個字串,青回傳滿足以下條件下最長字串的長度:
‘a’,‘b’,‘c’,‘x’,‘y’,'z’都恰好出現了偶數次(0也是偶數),
輸入:一個長度大于1的字串
輸出:一個整數,滿足條件的最長字串長度
思路:使用一個陣列來表示前綴和,使用二進制來表示哪些已經出現過,
AC100%
'''
給定一個字串,青回傳滿足以下條件下最長字串的長度:
'a','b','c','x','y','z'都恰好出現了偶數次(0也是偶數)
輸入:一個長度大于1的字串
輸出:一個整數,滿足條件的最長字串長度
'''
class Solution():
def solution(self,s):
tab = {'a':1, 'b':2, 'c':4, 'x':8, 'y':16, 'z':32}
now = 0
maxlen = 0
before = {0:-1}
for i in range(len(s)):
now = now if s[i] not in tab.keys() else now ^ tab[s[i]]
if now not in before.keys():
before[now] = i
maxlen = max(maxlen, i - before[now])
return maxlen
ss = Solution()
s = input()
res = ss.solution(s)
print(res)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/25203.html
標籤:其他
下一篇:目標檢測自動標注生成xml檔案
