diff --git a/best-time-to-buy-and-sell-stock/daehyun99.py b/best-time-to-buy-and-sell-stock/daehyun99.py new file mode 100644 index 0000000000..74d2624862 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/daehyun99.py @@ -0,0 +1,13 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + l = 0 + r = 0 + profit = 0 + + while r < len(prices): + if prices[l] < prices[r]: + profit = max(prices[r]-prices[l], profit) + elif prices[l] > prices[r]: + l = r + r += 1 + return profit diff --git a/encode-and-decode-strings/daehyun99.py b/encode-and-decode-strings/daehyun99.py new file mode 100644 index 0000000000..cdebf17b3c --- /dev/null +++ b/encode-and-decode-strings/daehyun99.py @@ -0,0 +1,31 @@ +class Solution: + + def encode(self, strs: List[str]) -> str: + encoded_strs = "" + for s in strs: + encoded_strs += str(len(s)) + encoded_strs += "#" + for char in s: + encoded_strs += char + return encoded_strs + + def decode(self, s: str) -> List[str]: + print(s) + decoded_strs = [] + + idx = 0 + while idx < len(s): + end = idx + 1 + while s[end] != "#": + end += 1 + length = s[idx:end] + idx = end + decoded_str = "" + for i in range(int(length)): + idx += 1 + decoded_str += s[idx] + idx += 1 + decoded_strs.append(decoded_str) + return decoded_strs + + diff --git a/group-anagrams/daehyun99.py b/group-anagrams/daehyun99.py new file mode 100644 index 0000000000..1fcdb475cf --- /dev/null +++ b/group-anagrams/daehyun99.py @@ -0,0 +1,38 @@ +from collections import defaultdict +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + groups = defaultdict(list) + + for s in strs: + count = [0] * 26 + + for c in s: + count[ord(c)-ord('a')] += 1 + + groups[tuple(count)].append(s) + return list(groups.values()) + +""" +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + def decode(s, key, base): + for c in s: + key[ord(c) - base] += 1 + result = "" + for k in key: + result += ' ' + str(k) + return result + keys = {} + base_key = [0 for i in range(26)] + base = ord("a") + + for s in strs: + key = decode(s, base_key.copy(), base) + temp = keys.get(key, []) + temp.append(s) + keys[key] = temp + results = [] + for val in keys.values(): + results.append(val) + return results +""" diff --git a/implement-trie-prefix-tree/daehyun99.py b/implement-trie-prefix-tree/daehyun99.py new file mode 100644 index 0000000000..c88af230f4 --- /dev/null +++ b/implement-trie-prefix-tree/daehyun99.py @@ -0,0 +1,43 @@ +class Trie: + def __init__(self): + self.root = dict() + + def insert(self, word: str) -> None: + pointer = self.root + for char in word: + if char in pointer: + pointer = pointer[char] + continue + else: + pointer[char] = dict() + pointer = pointer[char] + pointer[0] = dict() + + def search(self, word: str) -> bool: + pointer = self.root + for char in word: + if char in pointer: + pointer = pointer[char] + continue + else: + return False + if 0 in pointer: + return True + return False + + def startsWith(self, prefix: str) -> bool: + pointer = self.root + for char in prefix: + if char in pointer: + pointer = pointer[char] + continue + else: + return False + 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/daehyun99.py b/word-break/daehyun99.py new file mode 100644 index 0000000000..64ec706641 --- /dev/null +++ b/word-break/daehyun99.py @@ -0,0 +1,14 @@ +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + pos = set() + pos.add(0) + len_s = len(s) + + while len(pos) > 0 : + l = pos.pop() + for word in wordDict: + if s[l:].startswith(word): + pos.add(l+len(word)) + if l == len_s: + return True + return False