Fast & Simple Trick
There aren\'t that many combinations so you can always check all options via brute force. However, here\'s a clever trick.\n\n---\n\n\n# Intuition\nThe trick in
*There aren\'t that many combinations so you can always check all options via brute force. However, here\'s a clever trick.*\n\n---\n\n\n# Intuition\nThe trick involves thinking of a helpful commutative function in order to test for equality without caring for the actual order. i.e. think of a function with the following property: `f(x,y) = f(y,x)`.\n\n\n# Approach\nMultiplication works well for this problem. Just look at the corresponding ASCII values for each character pair, multiply them, then just compare the resulting values across the strings. \n\nThat is, check if:  `a[0] * a[2] == b[0] * b[2]`\n\nThis way you can implicitly check for equality of the characters regardless of the underlying order with one simple comparison. \n\nNote: You cannot use addition because you\'ll get accidental collisions. For example: `\'a\' + \'c\' == \'b\' + \'b\'`. However, since the range of ASCII values for lowercase letters is from 97-122, collisions are impossible with multiplication.\n\n# Complexity\n- Time complexity: `O(1)`\n\n- Space complexity: `O(1)`\n\n# Python Code\n```\nclass Solution(object):\n    def canBeEqual(self, a, b):\n        def mult(x, indices):\n            return ord(x[indices[0]]) * ord(x[indices[1]])\n        \n        return (mult(a, (0, 2)) == mult(b, (0, 2)) and \n                mult(a, (1, 3)) == mult(b, (1, 3)))\n\n```\n\n# Java Code\n```\nclass Solution {\n    public boolean canBeEqual(String s1, String s2) {\n        var a = s1.toCharArray();\n        var b = s2.toCharArray();\n        return (a[0] * a[2] == b[0] * b[2]) \n                 && (a[1] * a[3] == b[1] * b[3]);\n    }\n}\n```\n\n\n
['Math', 'String', 'Python', 'Java']