We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6a35335 commit 5c535dbCopy full SHA for 5c535db
1 file changed
encode-and-decode-strings/soobing2.ts
@@ -0,0 +1,42 @@
1
+/**
2
+ * 문제 유형
3
+ * - String
4
+ *
5
+ * 문제 설명
6
+ * - 문자열 인코딩과 디코딩
7
8
+ * 아이디어
9
+ * 1) "길이 + # + 문자열" 형태로 인코딩
10
11
+ */
12
+class Solution {
13
+ /**
14
+ * @param {string[]} strs
15
+ * @returns {string}
16
17
+ encode(strs) {
18
+ return strs.map((str) => `${str.length}#${str}`).join("");
19
+ }
20
+
21
22
+ * @param {string} str
23
+ * @returns {string[]}
24
25
+ decode(str) {
26
+ const result = [];
27
+ let tempStr = str;
28
+ while (tempStr.length) {
29
+ let i = 0;
30
31
+ while (tempStr[i] !== "#") {
32
+ i++;
33
34
35
+ const length = Number(tempStr.slice(0, i));
36
+ const currentStr = tempStr.slice(i + 1, i + 1 + length);
37
+ result.push(currentStr);
38
+ tempStr = tempStr.slice(length + i + 1);
39
40
+ return result;
41
42
+}
0 commit comments