add valid-anagram
This commit is contained in:
13
0242_valid-anagram/python3/nlogn.py
Normal file
13
0242_valid-anagram/python3/nlogn.py
Normal 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)
|
||||
8
0242_valid-anagram/python3/oneliner.py
Normal file
8
0242_valid-anagram/python3/oneliner.py
Normal 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)
|
||||
21
0242_valid-anagram/python3/solution.py
Normal file
21
0242_valid-anagram/python3/solution.py
Normal 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
|
||||
Reference in New Issue
Block a user