diff --git a/best-time-to-buy-and-sell-stock/Chanz82.py b/best-time-to-buy-and-sell-stock/Chanz82.py new file mode 100644 index 0000000000..c266919f09 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/Chanz82.py @@ -0,0 +1,20 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + + # set buy, sell as index of the prices + buy = 0 + sell = 1 + + max_profit = 0 + + while sell < len(prices): # loop until we checked last prices + if prices[buy] > prices[sell]: + buy = sell # found low price for buying + else : + profit = prices[sell] - prices[buy] + max_profit = max(profit, max_profit) + + sell += 1 # next sell price + + return max_profit + diff --git a/encode-and-decode-strings/Chanz82.py b/encode-and-decode-strings/Chanz82.py new file mode 100644 index 0000000000..e791550c5e --- /dev/null +++ b/encode-and-decode-strings/Chanz82.py @@ -0,0 +1,22 @@ +class Solution: + + def encode(self, strs: List[str]) -> str: + encoded_string = "" + + for plain_string in strs: + encoded_string = encoded_string + str(len(plain_string)) + ";" + plain_string + + #print(encoded_string) + return encoded_string + + def decode(self, s: str) -> List[str]: + decoded_str_list = [] + idx = 0 + while idx < len(s): + idx_del = s.index(";", idx) + #print(s[idx:idx_del]) + size = int(s[idx:idx_del]) + start_idx = idx_del+1 + decoded_str_list.append(s[start_idx:start_idx+size]) + idx = start_idx + size + return decoded_str_list diff --git a/group-anagrams/Chanz82.py b/group-anagrams/Chanz82.py new file mode 100644 index 0000000000..999fbd53e2 --- /dev/null +++ b/group-anagrams/Chanz82.py @@ -0,0 +1,11 @@ + +from collections import defaultdict +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + + result = defaultdict(list) + for word in strs: + key = ''.join(sorted(word)) + result[key].append(word) + + return list(result.values()) diff --git a/implement-trie-prefix-tree/Chanz82.py b/implement-trie-prefix-tree/Chanz82.py new file mode 100644 index 0000000000..d86520b0ec --- /dev/null +++ b/implement-trie-prefix-tree/Chanz82.py @@ -0,0 +1,45 @@ +class Trie: + + def __init__(self): + self.root_map = dict() + + def insert(self, word: str) -> None: + idx = 0 + search_map = self.root_map + while idx < len(word): + key = ord(word[idx]) + if key not in search_map: + search_map[key] = dict() + search_map = search_map[key] + idx += 1 + search_map['#'] = True + + def search(self, word: str) -> bool: + idx = 0 + search_map = self.root_map + while idx < len(word): + key = ord(word[idx]) + if key not in search_map: + return False + search_map = search_map[key] + idx += 1 + return '#' in search_map + + + def startsWith(self, prefix: str) -> bool: + idx = 0 + search_map = self.root_map + while idx < len(prefix): + key = ord(prefix[idx]) + if key not in search_map: + return False + search_map = search_map[key] + idx += 1 + return True + + +# Your Trie object will be instantiated and called as such: +# obj = Trie() +# obj.insert(word) +# param_2 = obj.search(word) +# param_3 = obj.startsWith(prefix) diff --git a/word-break/Chanz82.py b/word-break/Chanz82.py new file mode 100644 index 0000000000..2c0f54cb69 --- /dev/null +++ b/word-break/Chanz82.py @@ -0,0 +1,22 @@ +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + + visited = set() + + def dfs(start): + if start == len(s): + return True + + if start in visited: + return False + + for word in wordDict: + if s[start:start+len(word)] == word: + if dfs(start+len(word)): + return True + + visited.add(start) + return False + + return dfs(0) +