4 ms·
from collections import Counter if not Counter(str1) - Counter(str2): # success, they are anagrams
by BeFlatXIII 2y ago
from collections import Counter
if not Counter(str1) - Counter(str2): # success, they are anagrams
- xigoi 2y agostr1 = "a" str2 = "ab"
- BeFlatXIII 2y agoGood catch. Counter(str1) - Counter(str2) | Counter(str2) - Counter(str1) That ought to work. I'd hope Python's caching doesn't recompute the Counter for long strings.