LeetCode: Check If One String Swap Can Make Strings Equal Solution

Approach

Swap and unswap

Implementation

1/**
2 * @param {string} s1
3 * @param {string} s2
4 * @return {boolean}
5 */
6var areAlmostEqual = function (s1, s2) {
7 s2 = s2.split("")
8 for (let i = 0; i < s2.length; i++) {
9 for (let j = 0; j < s2.length; j++) {
10 ;[s2[i], s2[j]] = [s2[j], s2[i]]
11 if (s2.join("") === s1) {
12 return true
13 }
14 ;[s2[i], s2[j]] = [s2[j], s2[i]]
15 }
16 }
17 return false
18}

Comments

Loading comments...

Tags

leetcode

string

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Encode And Decode Tinyurl

HoningJS

Search Posts