Going from O(NlogN) -> O(N)
How\'s going Ladies-N-Gentlemen in this problem we are going to play with strings and all.\n\n# Brute Approach\n\nOkay, inorder to solve this problem the very f
How\'s going **Ladies-N-Gentlemen** in this problem we are going to play with strings and all.\n```\n# Brute Approach\n```\nOkay, inorder to solve this problem the very first approach coming in your mind is, **why don\'t we sort the Strings?**\n\n* I mean, yes you are saying right but, you can\'t sort the string that easily. \n* For that what we gonna do is, convert that string to character of array & then sort it.\n* So, this is a very brute force approach has to comes up in your mind,\n\n* So, after sorting the character we will compare "s" character with "t" character and if all are same return true else false\n\n\n```\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        if(s.length() != t.length()) return false;\n        \n        char cs[] = s.toCharArray();\n        char ct[] = t.toCharArray();\n        \n        Arrays.sort(cs);\n        Arrays.sort(ct);\n        \n        for(int i = 0; i < s.length(); i++){\n            if(cs[i] != ct[i]) return false;\n        }\n        return true;\n    }\n}\n```\nANALYSIS :-\n* **Time Complexity :-** **`BigO(NlogN)`**\n\n* **Space Complexity :-** **`BigO(N)`**\n\n<hr>\n<hr>\n\nLet\'s optimise it:\n```\n# Better Approach\n```\n\n<hr>\n<hr>\n\nTo optimise this solution what we going to do is, store these Strings **s** character in map and compare with **t** character if they got similar we going to reduce the frequency otherwise return false from there if not similar.\n\nSo, you can say we\'ll going to build a kind of **Frequency Map** of **`Character, Integer`**\n\n```\nclass Solution {\n    public boolean isAnagram(String s, String t) {\n        Map<Character, Integer> map = new HashMap<>();\n        \n        for(int i = 0; i < s.length(); i++){\n            map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);\n        }\n        \n        for(int i = 0; i < t.length(); i++){\n            if(map.containsKey(t.charAt(i)) == false) return false;\n            if(map.get(t.charAt(i)) > 1) map.put(t.charAt(i), map.get(t.charAt(i)) - 1);\n            else map.remove(t.charAt(i));\n        }\n        return map.size() == 0;\n    }\n}\n```\nANALYSIS :-\n* **Time Complexity :-** **`BigO(N)`**\n\n* **Space Complexity :-** **`BigO(N)`**
[]