Beginner Friendly: Super Easy and Simple Solution with O(1) time complexity
This is a beginner friendly code, you will definitely understand it if you properly read the intuition and approach.\nSo, please read intuition and approach bef
This is a beginner friendly code, you will definitely understand it if you properly read the intuition and approach.\nSo, please read intuition and approach before looking at the code.\n\n# Intuition\nThere can be maximum of two swaps:\n1] Index 0 and 2\n2] Index 1 and 3\nWe\'ll check if the elements at a particular index are equal or not. If not we\'ll swap them.\n\n# Approach\n1]Set two variables i = 0 and j = i+2 (as j-i=2).\n2]Run a for loop from i = 0 to i < 2. (as length of string is 4, so if i = 2, j = i+2 = 4)\n3]For each iteration check whether s1[i] != s2[i]. If true, then swap. So we\'ll check if the elements of both string are equal or not if equal then proceed with the loop if not then swap.\n4]When both strings get same i.e., s1 == s2, then exit from the loop.\n\n# Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution {\npublic:\n    bool canBeEqual(string s1, string s2) {\n        int i = 0, j = i + 2;\n        for(i = 0; i < 2; i++, j++)\n        {\n            if(s1[i] != s2[i])\n                swap(s1[i], s1[j]);\n\n            if(s1 == s2)\n                return 1;\n        }\n        return 0;\n    }\n};\n```\n\n\n![Upvote.jpeg](https://assets.leetcode.com/users/images/68ce34a1-50c5-4e07-a19a-de9107831390_1696937482.0277526.jpeg)\n\n\n
['String', 'String Matching', 'C++']