Title: #271 - Encode and Decode Strings - LeetCode JavaScript Solutions
Open Graph Title: #271 - Encode and Decode Strings - LeetCode JavaScript Solutions
X Title: #271 - Encode and Decode Strings - LeetCode JavaScript Solutions
Description: Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the ori...
Open Graph Description: Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the ori...
X Description: Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the ori...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/encode-and-decode-strings
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/encode-and-decode-strings","name":"#271 - Encode and Decode Strings - LeetCode JavaScript Solutions","description":"Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the ori...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * Encodes a list of strings to a single string.\n *\n * @param {string[]} strs\n * @return {string}\n */\nvar encode = function(strs) {\n return strs.map(str => `${str.length}:${str}`).join('');\n};\n\n/**\n * Decodes a single string to a list of strings.\n *\n * @param {string} s\n * @return {string[]}\n */\nvar decode = function(s) {\n const result = [];\n let i = 0;\n\n while (i < s.length) {\n const colon = s.indexOf(':', i);\n const len = parseInt(s.slice(i, colon));\n result.push(s.slice(colon + 1, colon + 1 + len));\n i = colon + 1 + len;\n }\n\n return result;\n};","keywords":"LeetCode 271, #271 - Encode and Decode Strings, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/encode-and-decode-strings |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/encode-and-decode-strings/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0271-encode-and-decode-strings.js |
| Array | https://leetcodejavascript.com/tags/array |
| String | https://leetcodejavascript.com/tags/string |
| Design | https://leetcodejavascript.com/tags/design |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow