Py3 C++計數bitmask與2 pointers速解Leetcode2273 Find Resultant Array After Removing Anagrams
字串長度最長也只有10,四個bits一組,用bitmask解題
-----
The maximum length of a string is only 10, and four bits are grouped together. Use bitmask to solve the problem.
[Py3 code請進]
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: def freq(s): ans=0 for c in s: i=ord(c)-97 ans+=(1<<4*i) return ans n, l=len(words), 0 ans=[words[0]] f0=freq(words[0]) for r in range(1, n): s=words[r] x=freq(s) if f0!=x: ans.append(s) l=r f0=x return ans
沒有留言:
張貼留言
HTML 編輯器