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 355a40c commit 9bc1fdcCopy full SHA for 9bc1fdc
encode-and-decode-strings/HC-kang.ts
@@ -0,0 +1,29 @@
1
+/**
2
+ * The most simple way
3
+ */
4
+function encode(strs: string[]): string {
5
+ return strs.join('🏖️');
6
+};
7
+function decode(s: string): string[] {
8
+ return s.split('🏖️');
9
10
+
11
+// T.C: O(n)
12
+// S.C: O(n)
13
14
+ return strs.map((s) => s.length + '#' + s).join('');
15
+}
16
17
18
19
20
+ const res: string[] = [];
21
+ let curIdx = 0;
22
+ while (curIdx < s.length) {
23
+ const sepIdx = s.indexOf('#', curIdx);
24
+ const len = parseInt(s.slice(curIdx, sepIdx), 10);
25
+ res.push(s.slice(sepIdx + 1, sepIdx + 1 + len));
26
+ curIdx = sepIdx + 1 + len;
27
+ }
28
+ return res;
29
0 commit comments