diff --git a/Algorithms/Easy/242_ValidAnagram/Solution.py b/Algorithms/Easy/242_ValidAnagram/Solution.py new file mode 100644 index 0000000..dffa7b0 --- /dev/null +++ b/Algorithms/Easy/242_ValidAnagram/Solution.py @@ -0,0 +1,16 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + count_t = {} + count_s = {} + for i in range(len(t)): + count_t[t[i]] = count_t.get(t[i], 0) + 1 + count_s[s[i]] = count_s.get(s[i], 0) + 1 + + for key in count_s.keys(): + if count_t.get(key) == None or count_t[key] != count_s[key]: + return False + + return True \ No newline at end of file diff --git a/Algorithms/Medium/49_GroupAnagrams/Solution.py b/Algorithms/Medium/49_GroupAnagrams/Solution.py new file mode 100644 index 0000000..be44af4 --- /dev/null +++ b/Algorithms/Medium/49_GroupAnagrams/Solution.py @@ -0,0 +1,18 @@ +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + grp_anagrams = {} + + for string in strs: + count = [0] * 26 + + for char in string: + count[ord(char) - ord("a")] += 1 + + count = tuple(count) + + if grp_anagrams.get(count) == None: + grp_anagrams[count] = [] + + grp_anagrams[count].append(string) + + return grp_anagrams.values() \ No newline at end of file