forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
364183b
commit 4a9da1e
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|