Skip to content

Commit

Permalink
Merge branch 'IbuAR-271' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
imaginate committed Jun 26, 2022
2 parents 96ae455 + 3e32ae9 commit 6c9a71d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions javascript/271-Encode-and-Decode-Strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {string[]} strs
* @return {string}
*/
function encode(strs) {
return strs.map(str => `${str.length}#${str}`).join('');
}

/**
* @param {string} str
* @return {string[]}
*/
function decode(str) {
const res = [];
let i = 0;

while (i < str.length) {
let j = i;
while (str[j] !== "#") {
++j;
}

const len = Number(str.slice(i, j));
res.push(str.slice(++j, j + len));
i = j + len;
}

return res;
}

0 comments on commit 6c9a71d

Please sign in to comment.