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:
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+
You can’t perform that action at this time.
0 commit comments