add valid-anagram

This commit is contained in:
2022-04-21 13:32:57 +05:30
parent 4c4bf0bd5f
commit 1578bc52d6
4 changed files with 64 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
# Time: O(NlogN)
# Space: O(1)
from collections import Counter
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
slen, tlen = len(s), len(t)
if slen != tlen:
return False
return sorted(s) == sorted(t)

View File

@@ -0,0 +1,8 @@
# Time: O(N)
# Space: O(N)
from collections import Counter
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
return Counter(s) == Counter(t)

View File

@@ -0,0 +1,21 @@
# Time: O(N) where N is num of chars in string
# Space: O(N) where N is num of chars in string
from collections import Counter
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
slen, tlen = len(s), len(t)
if slen != tlen:
return False
scounts, tcounts = {}, {}
for i in range(slen):
sc, tc = s[i], t[i]
scounts[sc] = scounts.get(sc, 0) + 1
tcounts[tc] = tcounts.get(tc, 0) + 1
return scounts == tcounts