Skip to content

Commit d3eba75

Browse files
committed
add Encode and Decode Strings solution
1 parent 98b697f commit d3eba75

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* [Problem]: [659] Encode and Decode Strings
3+
*
4+
* (https://www.lintcode.com/problem/659/)
5+
*/
6+
7+
//시간복잡도 O(n)
8+
//공간복잡도 O(1)
9+
10+
function encode(strs: string[]): string {
11+
return strs.join("😄");
12+
}
13+
function decode(s: string): string[] {
14+
return s.split("😄");
15+
}
16+
17+
// 시간복잡도: O(n)
18+
// 공간복잡도: O(n)
19+
function encode(strs: string[]): string {
20+
return strs.map((str) => `${str.length}:${str}`).join("");
21+
}
22+
23+
// 시간복잡도: O(n)
24+
// 공간복잡도: O(n)
25+
function decode(str: string): string[] {
26+
const result: string[] = [];
27+
let index = 0;
28+
while (index < str.length) {
29+
const separatorIndex = str.indexOf(":", index);
30+
const length = +str.slice(index, separatorIndex);
31+
const endIndex = separatorIndex + 1 + length;
32+
33+
result.push(str.slice(separatorIndex + 1, endIndex));
34+
index = endIndex;
35+
}
36+
return result;
37+
}

0 commit comments

Comments
 (0)