Skip to content

Commit eaa4ece

Browse files
committed
feat(soobing): encode-and-decode-strings
1 parent d4e6a58 commit eaa4ece

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
/**
3+
* ๋ฌธ์ž์—ด ๋ฐฐ์—ด์„ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ์ธ์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค.
4+
* @param strs - ๋ฌธ์ž์—ด ๋ฐฐ์—ด
5+
* @returns ์ธ์ฝ”๋”ฉ๋œ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด
6+
*/
7+
encode(strs: string[]): string {
8+
return strs.map((str) => str.length + "#" + str).join("");
9+
}
10+
11+
/**
12+
* ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด์„ ์›๋ž˜ ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋””์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค.
13+
* @param str - ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด
14+
* @returns ๋””์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด ๋ฐฐ์—ด
15+
*/
16+
decode(str: string): string[] {
17+
const result: string[] = [];
18+
19+
let i = 0;
20+
while (i < str.length) {
21+
let j = i;
22+
while (str[j] !== "#") {
23+
j++;
24+
}
25+
26+
const length = parseInt(str.slice(i, j));
27+
const word = str.slice(j + 1, j + 1 + length);
28+
result.push(word);
29+
i = j + 1 + length;
30+
}
31+
32+
return result;
33+
}
34+
}

0 commit comments

Comments
ย (0)