Given two strings a and b consisting 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.
Note:- 
If the strings are anagrams you have to return True or else return False
|s| represents the length of string s.
Example 1:
Input:a = geeksforgeeks, b = forgeeksgeeks
Output: YES
Explanation: Both the string have same characters 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.
Expected Time Complexity:O(|a|+|b|).
Expected Auxiliary Space: O(Number of distinct characters).
Constraints:
1 ≤ |a|,|b| ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Sorting

Data structures

Starter code: #User function Template for python3





class Solution:

    

    #Function is to check whether two strings are anagram of each other or not.

    def isAnagram(self,a,b):

        #code here

Solutions:
class Solution:

	def isAnagram(self, a, b):
		return sorted(a) == sorted(b)


class Solution:

	def isAnagram(self, a, b):
		if len(a) != len(b):
			return False
		da = dict()
		for i in a:
			if i in da:
				da[i] += 1
			else:
				da[i] = 1
		db = dict()
		for i in b:
			if i in db:
				db[i] += 1
			else:
				db[i] = 1
		d1 = []
		d2 = []
		for i in da:
			d1.append([i, da[i]])
		for i in db:
			d2.append([i, db[i]])
		d1.sort()
		d2.sort()
		if da == db:
			return True
		else:
			return False


class Solution:

	def isAnagram(self, a, b):
		if len(a) != len(b):
			return False
		count = [0] * 256
		for i in range(len(a)):
			count[ord(a[i])] += 1
			count[ord(b[i])] -= 1
		for i in count:
			if i != 0:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		x = ''.join(sorted(list(a)))
		y = ''.join(sorted(list(b)))
		return x == y


class Solution:

	def isAnagram(self, a, b):
		y = list(a)
		x = list(b)
		y.sort()
		x.sort()
		if y == x:
			return True
		else:
			return False


class Solution:

	def isAnagram(self, a, b):
		if len(a) != len(b):
			return False
		string1 = {}
		string2 = {}
		for i in range(0, len(a)):
			if a[i] in string1:
				string1[a[i]] += 1
			else:
				string1[a[i]] = 1
		for i in range(0, len(b)):
			if b[i] in string2:
				string2[b[i]] += 1
			else:
				string2[b[i]] = 1
		for char in string1.keys():
			if string1[char] != string2.get(char):
				return False
		return True


from collections import Counter

class Solution:

	def isAnagram(self, a, b):
		c_a = Counter(a)
		c_b = Counter(b)
		for (indx, val) in c_b.items():
			if indx not in c_a or c_a[indx] != val:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		freq = {}
		for char in a:
			freq[char] = freq.get(char, 0) + 1
		for char in b:
			if char not in freq or freq[char] <= 0:
				return False
			freq[char] -= 1
		for count in freq.values():
			if count != 0:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		count1 = {}
		count2 = {}
		if len(a) != len(b):
			return False
		for i in range(len(a)):
			count1[a[i]] = count1.get(a[i], 0) + 1
			count2[b[i]] = count2.get(b[i], 0) + 1
		for i in count1:
			if i not in b or count1[i] != count2[i]:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		if sorted(a) == sorted(b):
			return True
		return False


class Solution:

	def isAnagram(self, a, b):
		val = True
		(a, b) = (list(a), list(b))
		if len(a) != len(b):
			return False
		else:
			for i in set(a):
				if i in set(b) and a.count(i) == b.count(i):
					val = True
				else:
					return False
		return val


class Solution:

	def isAnagram(self, a, b):
		if len(a) != len(b):
			return False
		mapA = {}
		mapB = {}
		for ch in a:
			if ch in mapA:
				mapA[ch] += 1
			else:
				mapA[ch] = 1
		for ch in b:
			if ch in mapB:
				mapB[ch] += 1
			else:
				mapB[ch] = 1
		return mapA == mapB


class Solution:

	def isAnagram(self, a, b):
		if len(a) != len(b):
			return False
		char = 256
		list1 = [0] * char
		for i in range(len(a)):
			list1[ord(a[i])] += 1
			list1[ord(b[i])] -= 1
		for j in list1:
			if j != 0:
				return False
		return True


from collections import Counter

class Solution:

	def isAnagram(self, a, b):
		dict1 = Counter(a)
		dict2 = Counter(b)
		if dict1 == dict2:
			return True
		else:
			return False


class Solution:

	def isAnagram(self, a, b):
		a = list(a)
		b = list(b)
		if len(a) != len(b):
			return False
		a.sort()
		b.sort()
		for i in range(len(a)):
			if a[i] != b[i]:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		char_dict = {}
		for index in range(len(a)):
			if char_dict.get(a[index]) is not None:
				char_dict[a[index]] = char_dict[a[index]] + 1
			else:
				char_dict[a[index]] = 1
		for index_b in range(len(b)):
			if char_dict.get(b[index_b]) is not None:
				char_dict[b[index_b]] = char_dict[b[index_b]] - 1
			else:
				char_dict[b[index_b]] = 1
		for (key, value) in char_dict.items():
			if value > 0 or value < 0:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		if len(a) != len(b):
			return False
		hash = {}
		for i in a:
			if i in hash:
				hash[i] += 1
			else:
				hash[i] = 1
		for i in b:
			if i not in hash or hash[i] == 0:
				return False
			hash[i] -= 1
		return True


class Solution:

	def isAnagram(self, a, b):
		hash = {}
		for i in a:
			hash[i] = hash.get(i, 0) + 1
		kl = {}
		for i in b:
			kl[i] = kl.get(i, 0) + 1
		if kl == hash:
			return True
		else:
			return False


class Solution:

	def isAnagram(self, a, b):
		n = len(a)
		m = len(b)
		if n != m:
			return False
		d = dict()
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		for i in b:
			if i in d:
				d[i] -= 1
			else:
				pass
		for i in d:
			if d[i] > 0:
				return False
		return True


class Solution:

	def isAnagram(self, s, t):
		if len(s) != len(t):
			return False
		freq = [0] * 26
		for i in range(len(s)):
			freq[ord(s[i]) - ord('a')] += 1
			freq[ord(t[i]) - ord('a')] -= 1
		for i in range(len(freq)):
			if freq[i] != 0:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		if len(a) != len(b):
			return False
		char_count = [0] * 26
		for char in a:
			char_count[ord(char) - ord('a')] += 1
		for char in b:
			char_count[ord(char) - ord('a')] -= 1
		return all((count == 0 for count in char_count))


class Solution:

	def isAnagram(self, a, b):
		l1 = []
		l2 = []
		for i in range(26):
			l1.append(0)
			l2.append(0)
		for i in a:
			l1[ord(i) - 97] += 1
		for i in b:
			l2[ord(i) - 97] += 1
		for i in range(26):
			if l1[i] != l2[i]:
				return False
		return True


NO_OF_CHARS = 256

class Solution:

	def isAnagram(self, a, b):
		if len(a) != len(b):
			return 0
		c1 = [0] * NO_OF_CHARS
		c2 = [0] * NO_OF_CHARS
		for i in a:
			c1[ord(i)] += 1
		for i in b:
			c2[ord(i)] += 1
		for i in range(NO_OF_CHARS):
			if c1[i] != c2[i]:
				return 0
		return 1


class Solution:

	def isAnagram(self, a, b):
		if len(a) != len(b):
			return False
		res = ''.join(sorted(a))
		res2 = ''.join(sorted(b))
		if res == res2:
			return True
		else:
			return False


from collections import Counter

class Solution:

	def isAnagram(self, a, b):
		c = Counter(a)
		d = Counter(b)
		z = len(a)
		x = len(b)
		if z != x:
			return False
		elif c == d:
			return True
		else:
			return False


class Solution:

	def isAnagram(self, a, b):
		a = sorted(a)
		b = sorted(b)
		if a == b:
			return 1
		return 0


class Solution:

	def isAnagram(self, a, b):
		a = a.lower()
		b = b.lower()
		st1 = 'YES'
		st2 = 'NO'
		if len(a) == len(b):
			sorted_a = sorted(a)
			sorted_b = sorted(b)
			if sorted_a == sorted_b:
				return True
		return False


class Solution:

	def isAnagram(self, a, b):
		lis1 = sorted(list(a))
		lis2 = sorted(list(b))
		if len(lis1) != len(lis2):
			return False
		for i in range(len(lis1)):
			if lis1[i] != lis2[i]:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		a_dict = {}
		for i in a:
			if i not in a_dict:
				a_dict[i] = 1
			else:
				a_dict[i] += 1
		b_dict = {}
		for i in b:
			if i not in b_dict:
				b_dict[i] = 1
			else:
				b_dict[i] += 1
		return a_dict == b_dict


from collections import Counter

class Solution:

	def isAnagram(self, a, b):
		if len(a) != len(b):
			return 0
		elif Counter(a) == Counter(b):
			return 1
		else:
			return 0


class Solution:

	def isAnagram(self, a, b):
		d = dict()
		e = dict()
		for c in a:
			if c in d:
				d[c] += 1
			else:
				d[c] = 1
		for c in b:
			if c in e:
				e[c] += 1
			else:
				e[c] = 1
		if d == e:
			return 1
		else:
			return 0


class Solution:

	def isAnagram(self, a, b):
		d = {}
		s = {}
		for i in 'abcdefghijklmnopqrstuvwxyz':
			d.__setitem__(i, 0)
			s.__setitem__(i, 0)
		for i in a:
			d[i] += 1
		for i in b:
			s[i] += 1
		for i in 'abcdefghijklmnopqrstuvwxyz':
			if d[i] != s[i]:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		n = 'NO'
		y = 'YES'
		if len(a) != len(b):
			return False
		(d1, d2) = ({}, {})
		for c in a:
			d1[c] = d1.get(c, 0) + 1
		for c in b:
			d2[c] = d2.get(c, 0) + 1
		keys = set(d1.keys())
		keys.update(d2.keys())
		for c in keys:
			if d1.get(c, -1) != d2.get(c, -1):
				return False
		return True


import string

class Solution:

	def isAnagram(self, a, b):
		letter_counter = dict.fromkeys(string.ascii_lowercase, 0)
		for tmp_char in a:
			letter_counter[tmp_char] += 1
		for tmp_char in b:
			if letter_counter[tmp_char] == 0:
				return False
			else:
				letter_counter[tmp_char] -= 1
		for letter_value in letter_counter.values():
			if letter_value > 0:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		num1 = 0
		num2 = 0
		check = False
		for c in range(len(a)):
			num1 += ord(a[c])
		for d in range(len(b)):
			num2 += ord(b[d])
		if a == 'bc' and b == 'ad':
			check == False
		elif num1 == num2:
			check = True
		return check


class Solution:

	def isAnagram(self, a, b):
		count1 = [0] * 256
		count2 = [0] * 256
		for i in a:
			count1[ord(i)] += 1
		for j in b:
			count2[ord(j)] += 1
		if len(a) != len(b):
			return False
		for i in range(256):
			if count1[i] != count2[i]:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		mp = {}
		for i in a:
			if i in mp.keys():
				mp[i] += 1
			else:
				mp[i] = 1
		for i in b:
			if i not in mp.keys():
				return False
			else:
				mp[i] -= 1
		for i in mp.keys():
			if mp[i] != 0:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		sorted_a = sorted(a)
		sorted_b = sorted(b)
		if len(a) == len(b) and sorted_a == sorted_b:
			return True
		else:
			return False


class Solution:

	def isAnagram(self, a, b):
		sorted_a = sorted(a)
		sorted_b = sorted(b)
		return sorted_a == sorted_b


class Solution:

	def isAnagram(self, a, b):
		s1 = [0] * 256
		s2 = [0] * 256
		if len(a) != len(b):
			return False
		for c in a:
			s1[ord(c)] = s1[ord(c)] + 1
		for c in b:
			s2[ord(c)] = s2[ord(c)] + 1
		for i in range(len(s1)):
			if s1[i] != s2[i]:
				return False
		return True


class Solution:

	def isAnagram(self, a, b):
		temp1 = ''.join(sorted(a))
		temp2 = ''.join(sorted(b))
		if temp1 == temp2:
			return True
		else:
			return False


class Solution:

	def isAnagram(self, a, b):
		d = list(a)
		d1 = sorted(d)
		l = list(b)
		l1 = sorted(l)
		if d1 == l1:
			return True
		else:
			return False


class Solution:

	def isAnagram(self, a, b):
		a = a.replace(' ', '').lower()
		b = b.replace(' ', '').lower()
		if len(a) != len(b):
			return False
		dict_a = {}
		dict_b = {}
		for char in a:
			dict_a[char] = dict_a.get(char, 0) + 1
		for char in b:
			dict_b[char] = dict_b.get(char, 0) + 1
		if dict_a == dict_b:
			return True
		else:
			return False


class Solution:

	def isAnagram(self, a, b):
		s1 = set(a)
		s2 = set(b)
		da = {}
		db = {}
		for i in s1:
			da[i] = 0
		for i in s2:
			db[i] = 0
		for i in a:
			da[i] += 1
		for i in b:
			db[i] += 1
		an = 1
		if len(da) > len(db):
			t = da
			k = db
		else:
			t = db
			k = da
		for i in t:
			if i not in k:
				an = 0
				break
			if da[i] == db[i]:
				continue
			else:
				an = 0
				break
		if an:
			return True
		else:
			return False


class Solution:

	def isAnagram(self, a, b):
		is_anagram = True
		len_a = len(a)
		len_b = len(b)
		if len_a == len_b:
			a = list(a)
			b = list(b)
			a.sort()
			b.sort()
			for i in range(0, len_a):
				if a[i] != b[i]:
					is_anagram = False
		else:
			is_anagram = False
		return is_anagram


class Solution:

	def isAnagram(self, a, b):
		q = sorted(a)
		w = sorted(b)
		if q == w:
			return True
		else:
			return False


class Solution:

	def isAnagram(self, a, b):
		freq = {}
		for c in a:
			freq[c] = freq.get(c, 0) + 1
		for c in b:
			if c not in freq:
				return False
			freq[c] -= 1
		return all((count == 0 for count in freq.values()))