forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHoonDongKang.ts
More file actions
37 lines (32 loc) · 910 Bytes
/
HoonDongKang.ts
File metadata and controls
37 lines (32 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* [Problem]: [659] Encode and Decode Strings
*
* (https://www.lintcode.com/problem/659/)
*/
//시간복잡도 O(n)
//공간복잡도 O(1)
function encode(strs: string[]): string {
return strs.join("😄");
}
function decode(s: string): string[] {
return s.split("😄");
}
// 시간복잡도: O(n)
// 공간복잡도: O(n)
function encode(strs: string[]): string {
return strs.map((str) => `${str.length}:${str}`).join("");
}
// 시간복잡도: O(n)
// 공간복잡도: O(n)
function decode(str: string): string[] {
const result: string[] = [];
let index = 0;
while (index < str.length) {
const separatorIndex = str.indexOf(":", index);
const length = +str.slice(index, separatorIndex);
const endIndex = separatorIndex + 1 + length;
result.push(str.slice(separatorIndex + 1, endIndex));
index = endIndex;
}
return result;
}