Skip to content

Commit 1fe3839

Browse files
author
sangbeenmoon
committed
solved encode-and-decode-strings.
1 parent bb9b1c3 commit 1fe3839

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution:
2+
"""
3+
@param: strs: a list of strings
4+
@return: encodes a list of strings to a single string.
5+
"""
6+
def encode(self, strs):
7+
# write your code here
8+
result = ""
9+
10+
delimiter = ":;"
11+
12+
for i, str in enumerate(strs):
13+
result = result + str
14+
if i != len(strs):
15+
result = result + delimiter
16+
17+
print(result)
18+
19+
return result
20+
21+
"""
22+
@param: str: A string
23+
@return: decodes a single string to a list of strings
24+
"""
25+
def decode(self, str):
26+
result = []
27+
28+
i = 0
29+
delimiter = ":;"
30+
31+
while i < len(str):
32+
33+
j = i
34+
while j < len(str) - 1:
35+
if str[j:j+2] == delimiter:
36+
result.append(str[i:j])
37+
break
38+
else:
39+
j = j + 1
40+
41+
i = j + 2
42+
43+
return result

group-anagrams/sangbeenmoon.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# TC : O(m^2 * n) m : len(strs), n : len(strs[0])
2+
# SC : O(m * n)
3+
4+
class Solution:
5+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
6+
sorted_strs = []
7+
for str in strs:
8+
sorted_strs.append(sorted(str))
9+
10+
answer = []
11+
12+
i = 0
13+
14+
visited = [False] * 10001
15+
16+
while i < len(strs):
17+
if visited[i]:
18+
i = i + 1
19+
continue
20+
21+
sub_answer = []
22+
target = sorted_strs[i]
23+
sub_answer.append(strs[i])
24+
25+
for j in range(i+1, len(strs)):
26+
if sorted_strs[j] == target:
27+
visited[j] = True
28+
sub_answer.append(strs[j])
29+
30+
i = i + 1
31+
answer.append(sub_answer)
32+
33+
return answer
34+
35+

0 commit comments

Comments
 (0)