Skip to content

[Helena] Week 2 #727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions valid-anagram/yolophg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Time Complexity: O(n)
# Go through both strings once to count and compare characters.

# Space Complexity: O(1)
# The dictionary stores at most 26 letters, so space usage is constant.

class Solution:
def isAnagram(self, s: str, t: str) -> bool:
char_count = {}

# count frequencies of characters in string s
for char in s:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1

# go through 't' and decrease the count
for char in t:
# if the character isn't in the dictionary, it's not an anagram
if char in char_count:
char_count[char] -= 1
else:
return False

# check if all the counts are zero
for val in char_count.values():
if val != 0:
return False

return True
Loading