👏Beats 98.39% of users with Java🎉||✅Simple & Easy Well Explained Solution using Frequency🔥💥
Intuition\nThe code counts character frequencies in two strings, ensuring each character count matches for an anagram.\n\n# I Think This Can Help You(For Proof 
# Intuition\nThe code counts character frequencies in two strings, ensuring each character count matches for an anagram.\n\n# I Think This Can Help You(For Proof Click on the Image)\n[![Screenshot 2024-02-24 232407.png](https://assets.leetcode.com/users/images/1f96d603-0e99-44f4-bdba-70558aed386b_1714139621.4103026.png)](https://leetcode.com/problems/valid-anagram/submissions/1242517751/)\n\n# Approach\n1. **Method Signature**: \n   ```java\n   public boolean isAnagram(String s, String t)\n   ```\n   This method is named `isAnagram` and takes two input strings `s` and `t`. It returns a boolean value indicating whether `s` and `t` are anagrams of each other.\n\n2. **Initializing Frequency Array**: \n   ```java\n   int[] freq = new int[26];\n   ```\n   Here, an integer array `freq` of size 26 is created. This array is used to store the frequency of each character in the input strings. Each index corresponds to a character, with index 0 representing \'a\', index 1 representing \'b\', and so on.\n\n3. **Counting Frequencies in String `s`**:\n   ```java\n   for(char ch : s.toCharArray()){\n       freq[ch-\'a\'] += 1;\n   }\n   ```\n   This loop iterates through each character of string `s`. For each character, it calculates its position in the `freq` array by subtracting the ASCII value of \'a\' from the character\'s ASCII value. Then, it increments the count of that character in the `freq` array.\n\n4. **Decrementing Frequencies in String `t`**:\n   ```java\n   for(char ch : t.toCharArray()){\n       freq[ch-\'a\'] -= 1;\n   }\n   ```\n   Similar to the previous loop, this loop iterates through each character of string `t`. For each character, it decrements the count of that character in the `freq` array.\n\n5. **Checking if Frequencies are Zero**:\n   ```java\n   for(int i : freq){\n       if(i!=0){\n           return false;\n       }\n   }\n   ```\n   After processing both strings, this loop checks if there are any non-zero values in the `freq` array. If any non-zero value is found, it means that the frequencies of characters in `s` and `t` are not the same, so the method returns `false`, indicating that `s` and `t` are not anagrams.\n\n6. **Returning `true`**:\n   ```java\n   return true;\n   ```\n   If all characters have the same frequency in both strings, the method returns `true`, indicating that `s` and `t` are indeed anagrams of each other.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        int[] freq = new int[26];\n        for(char ch : s.toCharArray()){\n            freq[ch-\'a\'] += 1;\n        }\n        for(char ch : t.toCharArray()){\n            freq[ch-\'a\'] -= 1;\n        }\n        for(int i : freq){\n            if(i!=0){\n                return false;\n            }\n        }\n        return true;\n    }\n}\n```\n\n![_71eae110-0e71-4fc8-b986-1d9bcdd0a742.jpeg](https://assets.leetcode.com/users/images/de1005c7-6160-4ee4-8e16-a149de68937a_1714139553.993568.jpeg)\n
['Array', 'Hash Table', 'String', 'Sorting', 'Java']