File tree Expand file tree Collapse file tree 1 file changed +16
-11
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +16
-11
lines changed Original file line number Diff line number Diff line change @@ -4,26 +4,31 @@ class Solution:
4
4
# 공간복잡도: O(1)
5
5
def encode (self , strs ):
6
6
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 # 문자열 크기와 실제 문자열 사이에 구분자 '#'를 추가
11
10
12
11
return res
13
12
14
13
# 시간복잡도: O(N)
15
14
# 공간복잡도: O(N)
16
- def decode (self , str ):
15
+ def decode (self , s ):
17
16
idx = 0
18
- limit = len (str )
17
+ limit = len (s )
19
18
res = []
20
19
21
20
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 ]
27
30
res .append (text )
31
+ idx += num
28
32
29
33
return res
34
+
You can’t perform that action at this time.
0 commit comments