-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0271-encode-and-decode-strings.js
More file actions
130 lines (116 loc) · 3.35 KB
/
0271-encode-and-decode-strings.js
File metadata and controls
130 lines (116 loc) · 3.35 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* String - Delimiter
* Time O(N) | Space O(1)
* https://leetcode.com/problems/encode-and-decode-strings/
* @param {string[]} strs
* @return {string}
*/
var encode = (strs) => {
return strs
.map(
(str) => `${str.length}#${str}`,
) /* Time O(N) | Ignore Auxillary Space O(N) */
.join(''); /* Time O(N) | Ignore Auxillary Space O(N) */
};
/**
* String - Delimiter
* Time O(N * K) | Space O(N)
* https://leetcode.com/problems/encode-and-decode-strings/
* @param {string} str
* @return {string[]}
*/
var decode = (str, index = 0, decodedWords = []) => {
while (index < str.length) {
/* Time O(N) */
const { nextIndex, word } = delimitWord(
str,
index,
); /* Time O(K) | Ignore Auxillary Space Space (K) */
decodedWords.push(
word,
); /* | Ignore Auxillary Space O(N * K ) */
index = nextIndex;
}
return decodedWords;
};
const delimitWord = (str, index) => {
const delimiter = str.indexOf('#', index); /* Time O(K) */
const length = Number(str.slice(index, delimiter)); /* Time O(K) */
const [start, end] = [delimiter + 1, delimiter + length + 1];
const word = str.slice(
start,
end,
); /* Time O(K) | Ignore Auxillary Space O(K) */
return {
nextIndex: end,
word,
};
};
/**
* Non-ASCII Delimiter - Ignore Auxiliary Space
* Time O(N) | Space O(1)
* https://leetcode.com/problems/encode-and-decode-strings/
* @param {string[]} strs
* @return {string}
*/
var encode = (strs, nonASCIICode = String.fromCharCode(257)) => {
return strs.join(
nonASCIICode,
); /* Time O(N) | Ignore Auxillary Space O(N) */
};
/**
* Non-ASCII Delimiter - Ignore Auxiliary Space
* Time O(N) | Space O(1)
* https://leetcode.com/problems/encode-and-decode-strings/
* @param {string[]} strs
* @return {string}
*/
var decode = (strs, nonASCIICode = String.fromCharCode(257)) => {
return strs.split(
nonASCIICode,
); /* Time O(N) | Ignore Auxillary Space O(N) */
};
/**
* Chunk Transfer Encoding
* Time O(N) | Space O(1)
* https://leetcode.com/problems/encode-and-decode-strings/
* @param {string[]} strs
* @return {string}
*/
var encode = (strs, sb = []) => {
for (const str of strs) {
/* Time O(N) */
const code = getCode(str); /* Time O(1) */
const encoding = `${code}${str}`;
sb.push(encoding);
}
return sb.join(''); /* Time O(N) | Ignore Auxillary Space O(N) */
};
const getCode = (str) => str.length.toString(2).padStart(8, '0');
/**
* Chunk Transfer Encoding
* Time O(N) | Space O(1)
* https://leetcode.com/problems/encode-and-decode-strings/
* @param {string} str
* @return {string[]}
*/
var decode = (str, output = []) => {
for (
let left = 0, right = left + 8, length = 0;
left < str.length;
left = right + length, right = left + 8
) {
/* Time O(N) */
const countString = str.slice(
left,
right,
); /* | Ignore Auxillary Space O(K) */
length = parseInt(countString, 2);
const decoding = str.slice(
right,
right + length,
); /* Time O(K) | Ignore Auxillary Space O(N * K) */
output.push(decoding); /* | Ignore Auxillary Space O(N * K) */
}
return output;
};