Skip to content

Commit c88e490

Browse files
author
jinbeom
committed
fix week02 solution
1 parent 819606d commit c88e490

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

encode-and-decode-strings/kayden.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,31 @@ class Solution:
44
# 공간복잡도: O(1)
55
def encode(self, strs):
66
res = ""
7-
for str in strs:
8-
size = len(str)
9-
res += str(size)
10-
res += str
7+
for s in strs:
8+
size = len(s)
9+
res += str(size) + "#" + s # 문자열 크기와 실제 문자열 사이에 구분자 '#'를 추가
1110

1211
return res
1312

1413
# 시간복잡도: O(N)
1514
# 공간복잡도: O(N)
16-
def decode(self, str):
15+
def decode(self, s):
1716
idx = 0
18-
limit = len(str)
17+
limit = len(s)
1918
res = []
2019

2120
while idx < limit:
22-
num = str[idx]
23-
text = ""
24-
for _ in range(num):
25-
text += str[idx]
26-
idx+=1
21+
# 문자열 길이 파싱
22+
j = idx
23+
while s[j] != '#': # 구분자 '#'를 찾아 문자열 길이를 추출
24+
j += 1
25+
num = int(s[idx:j])
26+
idx = j + 1 # '#' 다음부터 실제 문자열 시작
27+
28+
# 실제 문자열 추출
29+
text = s[idx:idx + num]
2730
res.append(text)
31+
idx += num
2832

2933
return res
34+

0 commit comments

Comments
 (0)