from collections import Counter
class Solution:
def getHint(self, s: str, g: str) -> str:
a, b = 0, 0
cs, cg = Counter(), Counter()
for i in range(len(s)):
if s[i] == g[i]:
a += 1
continue
if cg[s[i]]:
cg[s[i]] -= 1
b += 1
else:
cs[s[i]] += 1
if cs[g[i]]:
cs[g[i]] -= 1
b += 1
else:
cg[g[i]] += 1
return f'{a}A{b}B'
2 posts tagged with "Hash Table"
View All Tags409. Longest Palindrome
class Solution:
def longestPalindrome(self, s: str) -> int:
r, pool = 0, set()
for c in s:
if c in pool:
pool.remove(c)
r += 1
else:
pool.add(c)
return r * 2 + bool(pool)