Two Pointers approach | O(n), O(1)
We need to iterate the chars in one (any) string and compare them with the chars in the other string.\nIf both s[i] == t[i] and s[i] == t[i + 2] are false, then
We need to iterate the chars in one (any) string and compare them with the chars in the other string.\nIf both `s[i] == t[i]` and `s[i] == t[i + 2]` are `false`, then the strings cannot be equal, and the solution returns `false`.\nIf `s[i] == t[i]` is `true`, then we simply move on to the next pair of chars.\nIf `s[i] == t[i + 2]` is `true`, then it means that there is a chance to make the strings equal, but we need to swap `t[i]` and `t[i + 2]` chars. What is important here is that we cannot swap the same pair more than once, so we need a way to mark a swap as a kind of *final*. So what we do is assign `t[i + 2] = t[i]` and then assign any char that is not a lowercase English letter to `t[i] = \'-\'`. It means that if we ever consider a char equal to `\'-\'` for a swap, that swap is impossible, and the strings are not equal.\n\n# Code\n```java []\nclass Solution {\n    public boolean canBeEqual(String s, String t) {\n\n        if (s.length() != t.length()) return false;\n\n        char[] sa = s.toCharArray();\n        char[] ta = t.toCharArray();\n        int len = sa.length;\n\n        int i = 0, j = 2;\n        while (i < len) {\n            if (sa[i] != ta[i]) {\n                if (sa[i] != ta[j]) {\n                    return false;\n                } else {\n                    ta[j] = ta[i];\n                    ta[i] = \'-\';\n                }\n            }\n            i++;\n            j = (j + 1) % len;\n        }\n        \n        return true;\n    }\n}\n```
['Two Pointers', 'Java']