✅ 🔥 0 ms Runtime Beats 100% User🔥||  Code Idea ✅ ||  Algorithm & Solving Step  ✅ ||
\n\n\n\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE :\n\n---\n\n### Bitwise XOR Approach :\n1. Key Insight:\n   - XOR (\( \oplus \)) is a bitwise operation th
![Screenshot 2024-11-22 at 9.26.09\u202FPM.png](https://assets.leetcode.com/users/images/dad310ac-869f-4306-8de5-a507e9b7a223_1732291183.9079177.png)\n\n\n\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE :\n\n---\n\n### **Bitwise XOR Approach** :\n1. **Key Insight**:\n   - XOR (\\( \\oplus \\)) is a bitwise operation that satisfies these properties:\n     - \\( a \\oplus a = 0 \\) (XOR-ing a number with itself results in 0).\n     - \\( a \\oplus 0 = a \\) (XOR-ing a number with 0 leaves it unchanged).\n   - If two strings are anagrams, XOR-ing all their characters will result in 0 because every character in one string will "cancel out" the same character in the other.\n\n2. **How It Works**:\n   - XOR all characters in both strings.\n   - If the result is 0, the strings are anagrams.\n   - Additionally, count the total character frequency to handle cases where strings of the same length have different characters but still result in XOR 0.\n\n---\n\n### **Algorithm Steps** :\n1. Check if the lengths of \\( s \\) and \\( t \\) are different. If yes, return `false`.\n2. Initialize:\n   - A variable `xorSum` to store the XOR result.\n   - An array `frequency` of size 26 to track character counts.\n3. Iterate through both strings simultaneously:\n   - XOR each character of \\( s \\) and \\( t \\) with `xorSum`.\n   - Increment the character count for \\( s \\).\n   - Decrement the character count for \\( t \\).\n4. Check:\n   - If `xorSum` is not 0, return `false`.\n   - If any value in the frequency array is not 0, return `false`.\n5. Otherwise, return `true`.\n\n---\n\n### **Complexity** :\n- **Time Complexity**: \\( O(n) \\)\n  - Single pass through both strings.\n- **Space Complexity**: \\( O(1) \\)\n  - Uses a fixed-size array of 26 and a single integer.\n\n---\n\n### **Implementation** :\n\n#### **C++**\n```cpp\n#include <string>\n#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n    bool isAnagram(string s, string t) {\n        if (s.length() != t.length()) return false;\n        \n        int xorSum = 0;\n        vector<int> frequency(26, 0);\n        \n        for (int i = 0; i < s.length(); i++) {\n            xorSum ^= s[i] ^ t[i]; // XOR both characters\n            frequency[s[i] - \'a\']++; // Increment for s\n            frequency[t[i] - \'a\']--; // Decrement for t\n        }\n        \n        if (xorSum != 0) return false; // If XOR isn\'t zero, they\'re not anagrams\n        \n        for (int freq : frequency) {\n            if (freq != 0) return false; // Check frequency counts\n        }\n        \n        return true;\n    }\n};\n```\n\n---\n\n#### **Java** :\n```java\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        if (s.length() != t.length()) return false;\n        \n        int xorSum = 0;\n        int[] frequency = new int[26];\n        \n        for (int i = 0; i < s.length(); i++) {\n            xorSum ^= s.charAt(i) ^ t.charAt(i); // XOR both characters\n            frequency[s.charAt(i) - \'a\']++; // Increment for s\n            frequency[t.charAt(i) - \'a\']--; // Decrement for t\n        }\n        \n        if (xorSum != 0) return false; // If XOR isn\'t zero, they\'re not anagrams\n        \n        for (int freq : frequency) {\n            if (freq != 0) return false; // Check frequency counts\n        }\n        \n        return true;\n    }\n}\n```\n\n---\n\n#### **Python 3** :\n```python\nclass Solution:\n    def isAnagram(self, s: str, t: str) -> bool:\n        if len(s) != len(t):\n            return False\n        \n        xor_sum = 0\n        frequency = [0] * 26\n        \n        for i in range(len(s)):\n            xor_sum ^= ord(s[i]) ^ ord(t[i]) # XOR both characters\n            frequency[ord(s[i]) - ord(\'a\')] += 1 # Increment for s\n            frequency[ord(t[i]) - ord(\'a\')] -= 1 # Decrement for t\n        \n        if xor_sum != 0: # If XOR isn\'t zero, they\'re not anagrams\n            return False\n        \n        if any(freq != 0 for freq in frequency): # Check frequency counts\n            return False\n        \n        return True\n```\n\n---\n\n### **Example Walkthrough** :\n\n#### Example 1\n- **Input**: \\( s = "anagram", t = "nagaram" \\)\n- **Process**:\n  - `xorSum = 0`, frequency initialized to zeros.\n  - After XOR and frequency updates, \\( xorSum = 0 \\), and all frequencies are zero.\n- **Output**: `true`.\n\n#### Example 2\n- **Input**: \\( s = "rat", t = "car" \\)\n- **Process**:\n  - XOR results in \\( xorSum \\neq 0 \\).\n- **Output**: `false`.\n\n![image.png](https://assets.leetcode.com/users/images/81976f6b-9916-4824-92ef-cdc358912aeb_1732290704.9690711.png)\n
['Hash Table', 'String', 'C++', 'Java', 'Python3']