diff --git a/best-time-to-buy-and-sell-stock/doh6077.py b/best-time-to-buy-and-sell-stock/doh6077.py new file mode 100644 index 000000000..42967c5e0 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/doh6077.py @@ -0,0 +1,18 @@ + +# 121. Best Time to Buy and Sell Stock +# O(n) - Two Pointers +class Solution: + def maxProfit(self, prices: list[int]) -> int: + left = 0 + right = 1 + profit = 0 + max_profit = 0 + + while right < len(prices): + if prices[right] > prices[left]: + profit = prices[right] - prices[left] + max_profit = max(profit, max_profit) + else: + left = right + right += 1 + return max_profit diff --git a/design-add-and-search-words-data-structure/doh6077.py b/design-add-and-search-words-data-structure/doh6077.py new file mode 100644 index 000000000..43db9fed5 --- /dev/null +++ b/design-add-and-search-words-data-structure/doh6077.py @@ -0,0 +1,32 @@ + +# 208. Implement Trie (Prefix Tree) +class Trie: + + def __init__(self): + self.list = [] + + + def insert(self, word: str) -> None: + self.list.append(word) + + + def search(self, word: str) -> bool: + if word in self.list: + return True + else: + return False + + + def startsWith(self, prefix: str) -> bool: + for i, value in enumerate(self.list): + if value.startswith(prefix): + return True + return False + + + +# 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/encode-and-decode-strings/doh6077.py b/encode-and-decode-strings/doh6077.py new file mode 100644 index 000000000..ef935478f --- /dev/null +++ b/encode-and-decode-strings/doh6077.py @@ -0,0 +1,26 @@ + + +# 271. Encode and Decode Strings + +# integer represents the following word's length +# read until we reach to delimiter + +class Solution: + def encode(self, strs): + res = "" + for s in strs: + res += str(len(s)) + "#" + s + return res + + def decode(self, str): + res, i = [], 0 + + while i < len(str): + j = i + while str[j] != "#": + j += 1 + length = int(str[i:j]) + res.append(str[j + 1 : j + 1 + length]) + i = j + 1 + length + + return res diff --git a/group-anagrams/doh6077.py b/group-anagrams/doh6077.py new file mode 100644 index 000000000..00db06852 --- /dev/null +++ b/group-anagrams/doh6077.py @@ -0,0 +1,16 @@ +from collections import defaultdict +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + # hashmap + # calculate frequency + # 26 alphabet characters + anagrams_dict = defaultdict(list) + for s in strs: + count = [0] * 26 + for c in s: + count[ord(c) - ord('a')] += 1 + + key = tuple(count) + anagrams_dict[key].append(s) + + return list(anagrams_dict.values()) diff --git a/word-break/doh6077.py b/word-break/doh6077.py new file mode 100644 index 000000000..fc20c0c03 --- /dev/null +++ b/word-break/doh6077.py @@ -0,0 +1,12 @@ +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + dp = [True] + [False] * len(s) + + for i in range(1, len(s) + 1): + for w in wordDict: + start = i - len(w) + if start >= 0 and dp[start] and s[start:i] == w: + dp[i] = True + break + + return dp[-1]