Skip to content

Commit

Permalink
sol1 for valid anagram
Browse files Browse the repository at this point in the history
  • Loading branch information
sahilverma-dev committed Dec 4, 2023
1 parent 9279bee commit dfbce65
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Arrays and Hashing/valid-anagram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# solution 1
def isAnagram(s: str, t: str) -> bool:
if len(s) == len(t):
map1 = {}
map2 = {}
arrayOfTrue = []

for x in s:
num = map1.get(x) or 0
map1[x] = num + 1

for x in t:
num = map2.get(x) or 0
map2[x] = num + 1

for key in map1.keys():
if map1.get(key) == map2.get(key):
arrayOfTrue.append(True)
else:
arrayOfTrue.append(False)

for key in map2.keys():
if map2.get(key) == map1.get(key):
arrayOfTrue.append(True)
else:
arrayOfTrue.append(False)

return not arrayOfTrue.__contains__(False)
else:
return False

# Example usages
print(isAnagram('sahil', 'shila')) # Output: True
print(isAnagram('sahil', 'lisah')) # Output: False

0 comments on commit dfbce65

Please sign in to comment.