Title: #394 - Decode String - LeetCode JavaScript Solutions
Open Graph Title: #394 - Decode String - LeetCode JavaScript Solutions
X Title: #394 - Decode String - LeetCode JavaScript Solutions
Description: Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brac...
Open Graph Description: Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brac...
X Description: Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brac...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/decode-string
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/decode-string","name":"#394 - Decode String - LeetCode JavaScript Solutions","description":"Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brac...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {string} s\n * @return {string}\n */\nvar decodeString = function(s) {\n const stack = [];\n let result = '';\n let decoder = 0;\n\n for (const c of s) {\n if (!isNaN(c) && Number(c) >= 0 && Number(c) <= 9) {\n decoder = Number(c) + decoder * 10;\n } else if (c === '[') {\n stack.push([result, decoder]);\n result = '';\n decoder = 0;\n } else if (c === ']') {\n const [previous, count] = stack.pop();\n result = previous + result.repeat(count);\n } else {\n result += c;\n }\n }\n\n return result;\n};","keywords":"LeetCode 394, #394 - Decode String, 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/decode-string |
| 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/decode-string/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0394-decode-string.js |
| String | https://leetcodejavascript.com/tags/string |
| Stack | https://leetcodejavascript.com/tags/stack |
| Recursion | https://leetcodejavascript.com/tags/recursion |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow