Anagram | Practice | GeeksforGeeks

Given two stringsaandbconsisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, act and tac are an anagram of each other.
Example 1:
Input:a = geeksforgeeks, b = forgeeksgeeks
Output: YES
Explanation: Both the string have samecharacters with
        same frequency. So, both are anagrams.
Example 2:
Input:a = allergy, b = allergic
Output: NO
Explanation:Characters in both the strings are 
        not same, so they are not anagrams.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isAnagram() which takes the string a and string b as input parameter and check if the two strings are an anagram of each other. The function returns true if the strings are anagram else it returns false.
Note: In python, you have to return True or False.
Expected Time Complexity:O(|a|+|b|).
Expected Auxiliary Space:O(Number of distinct characters).
Note: |s| represents the length of string s.
Constraints:
1 ≤ |a|,|b| ≤ 105
0
rrodwal22in 10 hours
int arr1[26] = {0};   int arr2[26] = {0};   int i = 0;   int len1 = a.length();   int len2 = b.length();   while(i < len1){       char ch = a[i];       arr1[ch - 'a'] += 1;       i++;   }      i = 0;      while(i < len2){       char ch = b[i];       arr2[ch - 'a'] += 1;       i++;   }      i = 0;   while(i < 26){       if(arr1[i] != arr2[i]){           return false;       }       i++;   }   return true;       
+1
arpitagarwal2854 hours ago
def isAnagram(self,a,b):       x = sorted(a)       y = sorted(b)       if x ==y:           return True       else:           return False
0
ashwanish9914 hours ago
Simple And Easy JAVA Solution
 
class Solution{       public static boolean isAnagram(String a,String b)   {       if(a.length() != b.length()){           return false;       }       char st1[] = a.toCharArray();       char st2[] = b.toCharArray();       Arrays.sort(st1);       Arrays.sort(st2);       if(Arrays.equals(st1, st2)){           return true;       }       return false;          }}
+1
manzoorhussain442816 hours ago
Answer in Python using:
def isAnagram(self,a,b):
        if len(a) != len(b):
            return False
        for s in set(a):
            if a.count(s) == b.count(s):
                continue
            else:
                return False
        return True
0
arshiavashisht0617 hours ago
C++ SOLUTION (USING unordered_map)
Time Complexity: linear.Auxiliary Space:O(Number of distinct characters).
 
bool isAnagram(string a, string b){              int n = a.length();       int m = b.length();       if(m!=n)           return false;       unordered_map<char, int> mp ;         for(int i=0; i<n; i++){           mp[a[i]]++;       }       for(int j=0; j<m; j++){           mp[b[j]]--;       }       for(auto it : mp){           if(it.second!=0)               return false;       }       return true;   }
0
carlosdegollado33322 hours ago
    public static boolean isAnagram(String a,String b)   {       if (a.length() != b.length()) {           return false;           } else {           boolean result = true;           Map<Character, Integer> hm = new HashMap<Character, Integer>();                      for (int i = 0; i < a.length(); i++) {               if (hm.containsKey(a.charAt(i))) {                   int count = hm.get(a.charAt(i));                   count++;                   hm.put(a.charAt(i), count);               } else {                   hm.put(a.charAt(i), 1);               }           }                      for (int k = 0; k < b.length(); k++) {               if (hm.containsKey(b.charAt(k))) {                   int count = hm.get(b.charAt(k));                   count--;                   hm.put(b.charAt(k), count);               } else {                   return false;               }           }                      for (Map.Entry<Character, Integer> me : hm.entrySet()) {               if (me.getValue() != 0) {                   return false;               }           }                      return result;       }   }
0
sshashikant1 day ago
JAVA | HashMap
T.C. - O(|a|+|b|)
S.C. - O(Number of distinct characters)
public static boolean isAnagram(String a,String b)
   {
       // Your code here
       if(a.length()!=b.length())
           return false;
           
       HashMap<Character, Integer> hm = new HashMap<>();
       
       for(char c : a.toCharArray()){
           hm.put(c, hm.getOrDefault(c,0)+1);
       }
       for(char c : b.toCharArray()){
           if(hm.containsKey(c))
               hm.put(c, hm.get(c)-1);
       }
       for(Map.Entry e : hm.entrySet()){
           if((int)e.getValue()!=0)
               return false;
       }
       return true;
0
negirs7xb2 days ago
My Solution : 
 bool isAnagram(string a, string b){              // Your code here       string c;
   int lenA = a.length();   int lenB  = b.length();
   char letter;
   if (lenA == lenB){       for (int i=0;i<lenA;i++){           for(int j=0;j<lenB; j++){               if (a.at(i) == b.at(j)){                   letter = a.at(i);                   c.append(1,letter);                   b.erase(b.begin() + j);                   lenB--;                   break;               }           }       }   }
   if (a==c){       return 1;   }else{       return 0;   }          }
0
anshikabansal3332 days ago
in this solution i have used 26 letters but the question asks for distinct elements as auxilliRY SPACE...IF SOMEONE CAN HELP?
 
// my code
 bool isAnagram(string a, string b){              // Your code here       int arr[26];       int barr[26];       for(int i=0;i<26;i++){           arr[i]=0;           barr[i]=0;       }       for(int i=0;i<a.length();i++){             int t=(int)a[i];             // cout<<t;            arr[t-97]++;                  }        for(int i=0;i<b.length();i++){            int t=(int)b[i];             barr[t-97]++;       }       for(int i=0;i<26;i++){           if(arr[i]!=barr[i]) return false;       }        return true;
}
 
 
 
0
ddttx772 days ago
	bool isAnagram(string a, string b)
    {
        if(a.size()!=b.size())
            return false;
        
        vector<int> v(26,0);
        int i;
        for(i=0;i<a.size();i++)
            v[a[i]-'0'-49]++;
        for(i=0;i<b.size();i++)
            v[b[i]-'0'-49]--;
        for(i=0;i<v.size();i++)
        {
            if(v[i]!=0)
                return false;
        }
        return true;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.