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
ravinuthalavamsikrishna14 hours ago
    def isAnagram(self,a,b):       #code here       l=[0]*26       l1=[0]*26       for i in a:           l[ord(i)-97]+=1       for i in b:           l1[ord(i)-97]+=1       if l==l1:           return True       return False
0
sachinsharma291113 days ago
def isAnagram(self,a,b):       #code here       x=list(a)       y=list(b)       x.sort()       y.sort()       if x==y:           return True       return False
+1
meenuchidam3 days ago
def isAnagram(self,a,b):       l1=list(a)       l2=list(b)       l1.sort()       l2.sort()       l1.sort()       c=str(l1)       d=str(l2)       if c==d:           return True       else:           return False
0
reeteshmeena
This comment was deleted.
0
surajraj207535 days ago
#java code
class Solution{       //Function is to check whether two strings are anagram of each other or not.   public static boolean isAnagram(String a,String b)   {       int p=a.length();       int q=a.length();       if(p==q){           char[] a1=a.toLowerCase().toCharArray();           char[] b1=b.toLowerCase().toCharArray();           Arrays.sort(a1);           Arrays.sort(b1);           if(Arrays.equals(a1,b1)){               return true;           }else{               return false;           }       }else{           return false;       }              // Your code here          }}
0
ayushmahobia015 days ago
bool isAnagram(string a, string b){              // Your code here       sort(a.begin(),a.end());       sort(b.begin(),b.end());              int n1= a.length();       int n2 =b.length();       if(n1!=n2)       {           return false;       }       for(int i=0;i<n1;i++){           if(a[i]!=b[i])           {               return false;           }       }       return true;   }
0
shashankshekhar8891 week ago
def isAnagram(self,a,b):
        for i in a:
            b=b.replace(i,"",1)
        if len(b)==0:
            return True
        else:
            return False
0
jay12501 week ago
#easy python 
class Solution:      #Function is to check whether two strings are anagram of each other or not.   def isAnagram(self,a,b):       #code here       return True if sorted(a) == sorted(b) else False
0
souminmohanty1 week ago
JAVA CODE 
------------
public static String sort(String a){      char temp[] = a.toCharArray();      Arrays.sort(temp);      return new String(temp);   }   public static boolean isAnagram(String a,String b)   {              // Your code here       String k = sort(a);       String m = sort(b);       if(k.equals(m))           return true;       else           return false;                 }
0
sailendrachettri1 week ago
C++ solution
bool isAnagram(string a, string b){
        
        // Your code here
        // cout<<a <<" "<<b<<endl;
        
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        
        // cout<<a <<" "<<b<<endl;
        
        if(a == b) return true;
        return false;
        
    }
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.