Updating exercises
This commit is contained in:
31
src/exercises/271.encode-and-decode-strings.js
Normal file
31
src/exercises/271.encode-and-decode-strings.js
Normal file
@@ -0,0 +1,31 @@
|
||||
// @leet start
|
||||
/**
|
||||
* Encodes a list of strings to a single string.
|
||||
*
|
||||
* @param {string[]} strs
|
||||
* @return {string}
|
||||
*/
|
||||
var key = '$#@C$(){(($[]%^[$()${#${(}'
|
||||
var encode = function(strs) {
|
||||
if (strs.length === 0) return [];
|
||||
const encoded = strs.join(key);
|
||||
return encoded;
|
||||
};
|
||||
|
||||
/**
|
||||
* Decodes a single string to a list of strings.
|
||||
*
|
||||
* @param {string} s
|
||||
* @return {string[]}
|
||||
*/
|
||||
var decode = function(str) {
|
||||
if (Array.isArray(str)) return str;
|
||||
let decoded = str.split(key);
|
||||
return decoded;
|
||||
};
|
||||
|
||||
/**
|
||||
* Your functions will be called as such:
|
||||
* decode(encode(strs));
|
||||
*/
|
||||
// @leet end
|
||||
Reference in New Issue
Block a user