Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
heypaprika committed Jan 9, 2025
1 parent 364183b commit 4a9da1e
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
21 changes: 21 additions & 0 deletions encode-and-decode-strings/heypaprika.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Big-O ์˜ˆ์ธก
# Time : O(n)
# Space : O(1)
class Codec:
def encode(self, strs: List[str]) -> str:
"""Encodes a list of strings to a single string.
"""
new_str = "-!@$@#!_".join(strs)
return new_str

def decode(self, s: str) -> List[str]:
"""Decodes a single string to a list of strings.
"""
return s.split("-!@$@#!_")



strs = ["Hello","World"]
codec = Codec()
codec.decode(codec.encode(strs))

23 changes: 23 additions & 0 deletions group-anagrams/heypaprika.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Big-O ์˜ˆ์ธก
# Time : O(k * nlog(n)) (k : strs ๋ฐฐ์—ด ์ˆ˜, n : strs ์›์†Œ์˜ unique ๋‹จ์–ด ๊ฐœ์ˆ˜)
# Space : O(k + n)
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
count_dict = {}
num = 0
dicted_list = []
for s in strs:
cur = str(sorted(Counter(s).items()))
dicted_list.append(cur)
if cur in count_dict:
continue
count_dict[cur] = num
num += 1

ans_list = [[] for _ in range(len(count_dict))]
for k, v in count_dict.items():
for i, dicted in enumerate(dicted_list):
if dicted == k:
ans_list[v].append(strs[i])
return ans_list

37 changes: 37 additions & 0 deletions implement-trie-prefix-tree/heypaprika.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Big-O ์˜ˆ์ธก
# Time : O(n) (n : ๋‹จ์–ด ๊ฐœ์ˆ˜)
# Space : O(n)
class Trie:

def __init__(self):
self.trie_dict = {}
self.trie_dict_list = [{}]

def insert(self, word: str) -> None:
self.trie_dict[word] = 1
for i in range(1, len(word) + 1):
if i > len(self.trie_dict_list) and i > 1:
self.trie_dict_list.append({})
self.trie_dict_list[i-1][word[:i]] = 1

def search(self, word: str) -> bool:
return word in self.trie_dict

def startsWith(self, prefix: str) -> bool:
if len(prefix) > len(self.trie_dict_list):
return False
return prefix in self.trie_dict_list[len(prefix)-1]


# Your Trie object will be instantiated and called as such:
trie = Trie()
trie.insert("apple")
trie.search("apple")
trie.search("app")
trie.startsWith("app")
trie.insert("app")
trie.search("app")

# param_2 = trie.search(word)
# param_3 = trie.startsWith(prefix)

31 changes: 31 additions & 0 deletions word-break/heypaprika.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Big-O ์˜ˆ์ธก
# Time : O(n^2) ์ตœ์•…์˜ ๊ฒฝ์šฐ wordDict์˜ ๊ฐœ์ˆ˜์˜ ์ œ๊ณฑ๋งŒํผ
# Space : O(n^2) ์ตœ์•…์˜ ๊ฒฝ์šฐ a์— wordDict์˜ ๊ฐœ์ˆ˜์˜ ์ œ๊ณฑ๋งŒํผ

# ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•œ ํ’€์ด
# ๋งจ ์•ž์˜ ๊ฒƒ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ๋‹ค ๊ณ ๋ฅด๊ธฐ (startswith)
# ๊ทธ๋“ค์„ ํ–ˆ์„ ๋•Œ์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ๋‹ค์Œ ๊ฒƒ์œผ๋กœ ๋™์ผํ•˜๊ฒŒ ๋ฐ˜๋ณต ์ง„ํ–‰ํ•˜๊ธฐ.

class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
a = [[s]]
i = 1
s_list = None
duplicate_dict = {}
while True:
s_list = a[-1]
a.append([])
for s in s_list:
for word in wordDict:
if s.startswith(word):
surplus_word = s[len(word):]
if surplus_word not in duplicate_dict:
a[-1].append(surplus_word)
duplicate_dict[surplus_word] = 1
# a[-1] = list(set(a[-1]))
if "" in a[-1]:
return True
if not a[-1]:
return False
i += 1

0 comments on commit 4a9da1e

Please sign in to comment.