Beginner Friendly :Unlocking the Code: Solving 'Check if Strings Can be Made Equal With Operation I'
Check if Strings Can be Made Equal With Operations I\n\n\n## Approach 1: Swapping Approach\n\n### Explanation\nThis approach swaps characters in s2 to check if 
# Check if Strings Can be Made Equal With Operations I\n\n\n## Approach 1: Swapping Approach\n\n### Explanation\nThis approach swaps characters in `s2` to check if it can be made equal to `s1`. It iterates through the first three characters of `s1` and compares them to the corresponding characters in `s2`. If they match, it continues. If not, it swaps the current character in `s2` with the character two positions ahead. Finally, it checks if the last two characters in both strings are equal.\n\n### Dry Run\nLet\'s dry run an example to illustrate how the approach works:\n\nInput:\n- s1 = "abcd"\n- s2 = "badc"\n\nIteration 1:\n- i = 0\n- s1[0] == s2[0], continue\n\nIteration 2:\n- i = 1\n- s1[1] == s2[1], continue\n\nIteration 3:\n- i = 2\n- s1[2] == s2[2], continue\n\nCheck last two characters:\n- s1[3] == s2[3], yes\n- s1[2] == s2[2], yes\n\nResult: s2 can be made equal to s1 by swapping.\n\n### Edge Cases\nThis approach handles cases where `s1` and `s2` are of length 4 and involve swapping operations.\n\n### Complexity Analysis\n- Time Complexity: O(1) - The approach always performs a fixed number of operations regardless of input size.\n- Space Complexity: O(1) - The approach uses a constant amount of extra space.\n\n### Codes in (C++) (Java) (Python) (C#) (JavaScript)\n\n```cpp []\nclass Solution {\npublic:\n    bool canBeEqual(string s1, string s2) {\n        for(int i=0;i< 2;i++){\n            if(s1[i] == s2[i] || s1[i] == s2[i+2] ){\n                if(s1[i] == s2[i]) continue;\n                else {\n                    swap(s2[i],s2[i+2]);\n                }\n            }\n            else{\n                return false;\n            }\n        }\n        \n        if(s1[3] == s2[3] && s1[2] == s2[2])\n            return true;\n        return false;\n    }\n};\n```\n\n```java []\nclass Solution {\n    public boolean canBeEqual(String s1, String s2) {\n        for (int i = 0; i < 2; i++) {\n            if (s1.charAt(i) == s2.charAt(i) || s1.charAt(i) == s2.charAt(i + 2)) {\n                if (s1.charAt(i) == s2.charAt(i)) continue;\n                else {\n                    char[] s2Chars = s2.toCharArray();\n                    char temp = s2Chars[i];\n                    s2Chars[i] = s2Chars[i + 2];\n                    s2Chars[i + 2] = temp;\n                    s2 = new String(s2Chars);\n                }\n            } else {\n                return false;\n            }\n        }\n\n        if (s1.charAt(3) == s2.charAt(3) && s1.charAt(2) == s2.charAt(2))\n            return true;\n        return false;\n    }\n}\n```\n\n```python []\nclass Solution:\n    def canBeEqual(self, s1: str, s2: str) -> bool:\n        for i in range(2):\n            if s1[i] == s2[i] or s1[i] == s2[i + 2]:\n                if s1[i] == s2[i]:\n                    continue\n                else:\n                    s2_list = list(s2)\n                    s2_list[i], s2_list[i + 2] = s2_list[i + 2], s2_list[i]\n                    s2 = \'\'.join(s2_list)\n            else:\n                return False\n        \n        if s1[3] == s2[3] and s1[2] == s2[2]:\n            return True\n        return False\n```\n\n```csharp []\npublic class Solution {\n    public bool CanBeEqual(string s1, string s2) {\n        for (int i = 0; i < 2; i++) {\n            if (s1[i] == s2[i] || s1[i] == s2[i + 2]) {\n                if (s1[i] == s2[i]) continue;\n                else {\n                    char[] s2Chars = s2.ToCharArray();\n                    char temp = s2Chars[i];\n                    s2Chars[i] = s2Chars[i + 2];\n                    s2Chars[i + 2] = temp;\n                    s2 = new string(s2Chars);\n                }\n            } else {\n                return false;\n            }\n        }\n\n        if (s1[3] == s2[3] && s1[2] == s2[2])\n            return true;\n        return false;\n    }\n}\n```\n\n```javascript []\nvar canBeEqual = function(s1, s2) {\n    for(let i = 0; i < 2; i++) {\n        if(s1[i] === s2[i] || s1[i] === s2[i + 2]) {\n            if(s1[i] === s2[i]) continue;\n            else {\n                let s2Arr = s2.split(\'\');\n                [s2Arr[i], s2Arr[i + 2]] = [s2Arr[i + 2], s2Arr[i]];\n                s2 = s2Arr.join(\'\');\n            }\n        } else {\n            return false;\n        }\n    }\n    \n    if(s1[3] === s2[3] && s1[2] === s2[2])\n        return true;\n    return false;\n};\n```\n\n| Language | Runtime | Memory |\n|----------|---------|--------|\n| C++      | 5 ms  | 6 MB |\n| Java     | 1 ms  | 41 MB |\n| Python   | 29 ms  | 16 MB |\n| C#       | 72 ms  | 43 MB |\n| JavaScript | 63 ms | 44 MB |\n\n---\n\n\n# Consider UPVOTING\u2B06\uFE0F\n\n![image.png](https://assets.leetcode.com/users/images/00ac1cc1-8649-46e7-a76e-8274c086e8cb_1693579192.601051.png)\n\n# DROP YOUR SUGGESTIONS IN THE COMMENT\n\n## Keep Coding\uD83E\uDDD1\u200D\uD83D\uDCBB\n\n -- *MR. ROBOT SIGNING OFF*\n
['Greedy', 'Brainteaser', 'String Matching', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']