Files
leetcode/src/exercises/271.encode-and-decode-strings.js
2025-07-14 10:09:13 -04:00

32 lines
606 B
JavaScript

// @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