1790. Check if One String Swap Can Make Strings Equal

🟩 Easy

Solution

My Solution

func areAlmostEqual(s1 string, s2 string) bool {
    if len(s1) != len(s2) {
        return false
    }
    var (
        has, equal bool
        idx int
    )

    for i, s := range s1 {
        if s == rune(s2[i]) {
            continue
        }    

        if has {
            if equal {
                return false
            }
    
            if rune(s2[idx]) == s && s2[i] == s1[idx] {
                equal = true
            } else {
                return false
            }
        }

        has = true
        idx = i
    }
    if has {
        return equal
    }
    return true
}
result

Leetcode: link

Last updated

Was this helpful?