BEST EXPANATION✅ || 💯 % RUNTIME🔥 || IN 3 LANGUAGE 🧑‍💻|| BEGINEER FRIENDLY🍵 || 2 POINTER🅿️🅿️
Intuition \uD83D\uDCA1\nMy initial thoughts were to compare characters from both strings and swap if necessary to make them equal.\n\n# Approach \uD83D\uDEE0\uF
# Intuition \uD83D\uDCA1\nMy initial thoughts were to compare characters from both strings and swap if necessary to make them equal.\n\n# Approach \uD83D\uDEE0\uFE0F\nI utilized a while loop to iterate through both strings, comparing characters. If characters didn\'t match, I swapped them (if possible) in `ch1`. After the loop, I checked if both arrays were equal.\n\n# Complexity \uD83D\uDCCA\n- Time complexity: \n  - $$O(n)$$ where $$n$$ is the length of the strings `s1` and `s2`.\n- Space complexity:\n  - $$O(1)$$ as no extra space is used besides the input arrays.\n\n# Code\n```java []\nclass Solution {\n    public boolean canBeEqual(String s1, String s2) {\n        char[] ch1 = s1.toCharArray();\n        char[] ch2 = s2.toCharArray();\n        int i = 0;\n        while(i < ch1.length){\n            if(ch1[i] == ch2[i]){\n                i++;\n            }\n            else{\n                if(i+2 < ch1.length){\n                    char temp = ch1[i];\n                    ch1[i] = ch1[i+2];\n                    ch1[i+2] = temp;\n                }\n                i++;\n            }\n        }\n        for(int k=0;k<ch1.length;k++){\n            if(ch1[k] != ch2[k]) return false;\n        }\n        return true;\n    }\n}\n```\n```javascript []\n/**\n * @param {string} s1\n * @param {string} s2\n * @return {boolean}\n */\nvar canBeEqual = function(s1, s2) {\n    let ch1 = [...s1];\n    let ch2 = [...s2];\n    let i=0;\n    while(i < s1.length){\n        if(ch1[i] === ch2[i]){\n            i++;\n        }\n        else{\n            if(i+2 < s1.length){\n                let temp = ch1[i];\n                ch1[i] = ch1[i+2];\n                ch1[i+2] = temp;\n            }\n            i++;\n        }\n    }\n    for(let j=0;j<s1.length;j++){\n        if(ch1[j] !== ch2[j]) return false;\n    }\n    return true;\n};\n```\n```C++ []\nclass Solution {\npublic:\n    bool canBeEqual(string s1, string s2) {\n        vector<char> ch1(s1.begin(), s1.end());\n        vector<char> ch2(s2.begin(), s2.end());\n        int i = 0;\n        while (i < ch1.size()) {\n            if (ch1[i] == ch2[i]) {\n                i++;\n            } else {\n                if (i + 2 < ch1.size()) {\n                    char temp = ch1[i];\n                    ch1[i] = ch1[i + 2];\n                    ch1[i + 2] = temp;\n                }\n                i++;\n            }\n        }\n        for (int k = 0; k < ch1.size(); k++) {\n            if (ch1[k] != ch2[k])\n                return false;\n        }\n        return true;\n    }\n};\n```\n![image.png](https://assets.leetcode.com/users/images/06ffa3b3-6d4c-42b6-8793-6407f2625dbf_1710428672.6311295.png)\n\n\n          -------------------------HAPPY CODING -------------------\n
['Two Pointers', 'String', 'C++', 'Java', 'JavaScript']