diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..c46734dc --- /dev/null +++ b/problem1.py @@ -0,0 +1,16 @@ +## Problem 1: +## Given an array of strings, group anagrams together. + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + final_dict = {} + + for str_vals in strs: + a = [x for x in str_vals] + a.sort() + a = ''.join(a) + if a not in final_dict: + final_dict[a]=[str_vals] + else: + final_dict[a].append(str_vals) + return list(final_dict.values()) \ No newline at end of file diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..be370415 --- /dev/null +++ b/problem2.py @@ -0,0 +1,22 @@ +## Problem 2: +#Given two strings s and t, determine if they are isomorphic. + +class Solution: + def isIsomorphic(self, s: str, t: str) -> bool: + map_dict = { + + } + check_set = set() + if len(s) != len(t): + return False + for i in range(len(s)): + if s[i] not in map_dict: + if t[i] not in check_set: + map_dict[s[i]]=t[i] + check_set.add(t[i]) + else: + return False + else: + if map_dict[s[i]] != t[i]: + return False + return True \ No newline at end of file diff --git a/problem3.py b/problem3.py new file mode 100644 index 00000000..fb6c8ce0 --- /dev/null +++ b/problem3.py @@ -0,0 +1,22 @@ +# https://leetcode.com/problems/word-pattern/ + +class Solution: + def wordPattern(self, pattern: str, s: str) -> bool: + my_hash = { + + } + my_set = set() + if len([x for x in pattern]) != len(s.split(' ')): + return False + for s_map,w_map in zip([x for x in pattern],s.split(' ')): + if s_map in my_hash: + if my_hash[s_map] != w_map: + return False + else: + if w_map in my_set: + return False + else: + my_hash[s_map] = w_map + my_set.add(w_map) + return True + \ No newline at end of file