Title: #859 - Buddy Strings - LeetCode JavaScript Solutions
Open Graph Title: #859 - Buddy Strings - LeetCode JavaScript Solutions
X Title: #859 - Buddy Strings - LeetCode JavaScript Solutions
Description: Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false. Swapping...
Open Graph Description: Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false. Swapping...
X Description: Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false. Swapping...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/buddy-strings
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/buddy-strings","name":"#859 - Buddy Strings - LeetCode JavaScript Solutions","description":"Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false. Swapping...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {string} s\n * @param {string} goal\n * @return {boolean}\n */\nvar buddyStrings = function(s, goal) {\n if (s.length !== goal.length) return false;\n\n const differences = [];\n const charCount = new Map();\n let hasDuplicate = false;\n\n for (let i = 0; i < s.length; i++) {\n if (s[i] !== goal[i]) {\n differences.push(i);\n if (differences.length > 2) return false;\n }\n charCount.set(s[i], (charCount.get(s[i]) || 0) + 1);\n if (charCount.get(s[i]) > 1) hasDuplicate = true;\n }\n\n if (differences.length === 0) return hasDuplicate;\n if (differences.length !== 2) return false;\n\n const [first, second] = differences;\n return s[first] === goal[second] && s[second] === goal[first];\n};","keywords":"LeetCode 859, #859 - Buddy Strings, Easy, 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/buddy-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/buddy-strings/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0859-buddy-strings.js |
| String | https://leetcodejavascript.com/tags/string |
| Hash Table | https://leetcodejavascript.com/tags/hash-table |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow