forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoobing.ts
More file actions
34 lines (30 loc) ยท 824 Bytes
/
soobing.ts
File metadata and controls
34 lines (30 loc) ยท 824 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
class Solution {
/**
* ๋ฌธ์์ด ๋ฐฐ์ด์ ํ๋์ ๋ฌธ์์ด๋ก ์ธ์ฝ๋ฉํฉ๋๋ค.
* @param strs - ๋ฌธ์์ด ๋ฐฐ์ด
* @returns ์ธ์ฝ๋ฉ๋ ํ๋์ ๋ฌธ์์ด
*/
encode(strs: string[]): string {
return strs.map((str) => str.length + "#" + str).join("");
}
/**
* ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด์ ์๋ ๋ฌธ์์ด ๋ฐฐ์ด๋ก ๋์ฝ๋ฉํฉ๋๋ค.
* @param str - ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด
* @returns ๋์ฝ๋ฉ๋ ๋ฌธ์์ด ๋ฐฐ์ด
*/
decode(str: string): string[] {
const result: string[] = [];
let i = 0;
while (i < str.length) {
let j = i;
while (str[j] !== "#") {
j++;
}
const length = parseInt(str.slice(i, j));
const word = str.slice(j + 1, j + 1 + length);
result.push(word);
i = j + 1 + length;
}
return result;
}
}