Title: #87 - Scramble String - LeetCode JavaScript Solutions
Open Graph Title: #87 - Scramble String - LeetCode JavaScript Solutions
X Title: #87 - Scramble String - LeetCode JavaScript Solutions
Description: We can scramble a string s to get a string t using the following algorithm: If the length of the string is 1, stop. If the length of the st...
Open Graph Description: We can scramble a string s to get a string t using the following algorithm: If the length of the string is 1, stop. If the length of the st...
X Description: We can scramble a string s to get a string t using the following algorithm: If the length of the string is 1, stop. If the length of the st...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/scramble-string
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/scramble-string","name":"#87 - Scramble String - LeetCode JavaScript Solutions","description":"We can scramble a string s to get a string t using the following algorithm: If the length of the string is 1, stop. If the length of the st...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {string} s1\n * @param {string} s2\n * @return {boolean}\n */\nvar isScramble = function(s1, s2) {\n const n = s1.length;\n const dp = new Array(n).fill().map(() => new Array(n).fill().map(() => new Array(n).fill(null)));\n\n function dfs(i1, j1, i2) {\n if (dp[i1][j1][i2] !== null) {\n return dp[i1][j1][i2];\n }\n if (i1 == j1) {\n return dp[i1][j1][i2] = s1[i1] === s2[i2];\n }\n\n const j2 = j1 - i1 + i2;\n let result = false;\n for (let i = i1; i < j1; i++) {\n result = result || (dfs(i1, i, i2) && dfs(i + 1, j1, i2 + i - i1 + 1))\n || (dfs(i1, i, j2 - i + i1) && dfs(i + 1, j1, i2));\n }\n\n return dp[i1][j1][i2] = result;\n }\n\n return dfs(0, n - 1, 0);\n};","keywords":"LeetCode 87, #87 - Scramble String, Hard, 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/scramble-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/scramble-string/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0087-scramble-string.js |
| String | https://leetcodejavascript.com/tags/string |
| Dynamic Programming | https://leetcodejavascript.com/tags/dynamic-programming |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow