> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/1790.-check-if-one-string-swap-can-make-strings-equal.md).

# 1790. Check if One String Swap Can Make Strings Equal

&#x20;compare to 859 buddy strings and to see their differences.

```
// My method
class Solution {
    public boolean areAlmostEqual(String s1, String s2) {
        if (s1.length() != s2.length())
            return false;
        if (s1.equals(s2)) 
            return true;
        char[] ch1 = s1.toCharArray();
        char[] ch2 = s2.toCharArray();
        HashMap<Character,Integer> map1 = new HashMap<>();
        HashMap<Character,Integer> map2 = new HashMap<>();
        
        for (char ch: ch1){
            map1.put(ch, map1.getOrDefault(ch,0) + 1);
        }
        
         for (char ch: ch2){
            map2.put(ch, map2.getOrDefault(ch,0) + 1);
        }
        
        if (!map1.entrySet().equals(map2.entrySet())) return false;
        int dif = 0;
        for (int i = 0; i < ch1.length; i++){
            if (ch1[i] != ch2[i]){
                dif++;
                if (dif > 2) return false;
            }
        }
        return true;
    }
}
```

Optimized 1:

```
// Some code
class Solution {
    public boolean areAlmostEqual(String s1, String s2) {
        if (s1.length() != s2.length())
            return false;
        if (s1.equals(s2)) 
            return true;
        int first = -1;
        int second = -1;
        
        for (int i = 0; i < s1.length(); i++){
            if (s1.charAt(i) != s2.charAt(i)){
                if (first == -1){
                    first = i;
                } else if (second == -1){
                    second = i;
                } else {
                    return false;
                }
            }
        }
        
        if (first == -1) 
            return true; //equal string;
        else if (second == -1)
            return false;// 1 mismatch
        else
            return (s1.charAt(first) == s2.charAt(second) && s1.charAt(second) == s2.charAt(first));
    }
}
```

```
// Some code
class Solution {
    public boolean areAlmostEqual(String s1, String s2) {
     
      int[] s1Array = new int[26];
      int[] s2Array = new int[26];
      int counter = 0;
      for(int i = 0;i<s1.length();i++){
         char s = s1.charAt(i);
         char ss = s2.charAt(i);
         if(s != ss)
            counter++;
         if(counter > 2)
            return false;
          s1Array[s -'a']++;
         s2Array[ss -'a']++;
      }
      return Arrays.equals(s1Array, s2Array);
    }
}
```

```
// Some code
class Solution {
    public boolean areAlmostEqual(String s1, String s2) {
        if (s1.length() != s2.length())
            return false;
        if (s1.equals(s2)) 
            return true;
        int[] letters = new int[26];
        int count = 0;
        for (int i = 0; i < s1.length(); i++){
            if (s1.charAt(i) != s2.charAt(i)){
                count++;
            if (count > 2) return false;
            letters[s1.charAt(i) - 'a']++;
            letters[s2.charAt(i) - 'a']--;     
            }
        }
        
        for (int n: letters){
            if (n != 0) return false;
        }
        
        return count == 0 || count == 2;
        
    }
}
```
