forked from YuriSpiridonov/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
751529b
commit 7abccbb
Showing
3 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
''' | ||
Given two strings s and t , write a function to determine | ||
if t is an anagram of s. | ||
Example: | ||
Input: s = "anagram", t = "nagaram" | ||
Output: true | ||
Example: | ||
Input: s = "rat", t = "car" | ||
Output: false | ||
Note: | ||
You may assume the string contains only lowercase alphabets. | ||
Follow up: | ||
What if the inputs contain unicode characters? How would | ||
you adapt your solution to such case? | ||
''' | ||
#Dfficulty: Easy | ||
#34 / 34 test cases passed. | ||
#Runtime: 32 ms | ||
#Memory Usage: 14.5 MB | ||
|
||
#Runtime: 32 ms, faster than 98.22% of Python3 online submissions for Valid Anagram. | ||
#Memory Usage: 14.5 MB, less than 48.96% of Python3 online submissions for Valid Anagram. | ||
|
||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
if not s and not t: return True | ||
if not s or not t or len(s) != len(t): return False | ||
i = 0 | ||
c = set(s+t) | ||
result = False | ||
for l in c: | ||
if l in s and l in t and s.count(l) == t.count(l): | ||
result = True | ||
continue | ||
else: | ||
result = False | ||
break | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters