Given two strings s and t , write a function to determine if t is an anagram of s.

Example 1:


Input: s = "anagram", t = "nagaram"
Output: true


Example 2:


Input: s = "rat", t = "car"
Output: false


Note:
You may assume the string contains only lowercase alphabets.

Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?

Source: leetcode | Difficulty: EASY
Tags: String algorithms

Sorting

Data structures

Starter code: class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        

Solutions:
class Solution:

	def isAnagram(self, s, t):
		if len(s) != len(t):
			return False
		if s == t:
			return True
		for i in map(chr, range(97, 123)):
			if s.count(i) != t.count(i):
				return False
		return True


class Solution:

	def isAnagram(self, s, t):
		if len(s) != len(t):
			return False
		ssort = sorted(list(s))
		tsort = sorted(list(t))
		return ssort == tsort


class Solution:

	def isAnagram(self, s, t):
		if len(s) != len(t):
			return False
		s_list = list(s)
		t_list = list(t)
		s_list.sort()
		t_list.sort()
		if s_list == t_list:
			return True
		return False


class Solution:

	def isAnagram(self, s, t):

		def cnt(S):
			c = {}
			for i in S:
				if i in c:
					c[i] += 1
				else:
					c[i] = 1
			return c
		c1 = cnt(s)
		c2 = cnt(t)
		return c2 == c1


class Solution:

	def isAnagram(self, s, t):
		(memoS, memoT) = ({}, {})
		for c in s:
			if c not in memoS:
				memoS[c] = 0
			memoS[c] += 1
		for c in t:
			if c not in memoT:
				memoT[c] = 0
			memoT[c] += 1
		for v in memoS:
			if v not in memoT or memoT[v] != memoS[v]:
				return False
		for v in memoT:
			if v not in memoS or memoS[v] != memoT[v]:
				return False
		return True


class Solution:

	def isAnagram(self, s, t):
		if len(s) != len(t):
			return False
		charMap = {}
		for char in s:
			if char in charMap:
				charMap[char] += 1
			else:
				charMap[char] = 1
		for char in t:
			if char in charMap and charMap[char] > 0:
				charMap[char] -= 1
			else:
				return False
		return True


class Solution:

	def isAnagram(self, s, t):
		S = set(s)
		T = set(t)
		if len(s) != len(t):
			return False
		for i in T:
			if i in S:
				if s.count(i) != t.count(i):
					return False
			else:
				return False
		return True


class Solution:

	def isAnagram(self, s, t):
		word = dict()
		for i in s:
			if i not in word:
				word[i] = 1
			else:
				word[i] += 1
		for i in t:
			if i not in word:
				return False
			else:
				word[i] -= 1
		for i in word:
			if word[i] != 0:
				return False
		return True


class Solution:

	def isAnagram(self, s, t):
		ls = self.makeDict(s)
		lt = self.makeDict(t)
		if len(list(ls.keys())) > len(list(lt.keys())):
			for (ks, vs) in list(ls.items()):
				if not (ks in lt and lt[ks] == vs):
					return False
			return True
		else:
			for (kt, vt) in list(lt.items()):
				if not (kt in ls and ls[kt] == vt):
					return False
			return True

	def makeDict(self, s):
		ls = {}
		for l in s:
			if l in ls:
				ls[l] += 1
			else:
				ls[l] = 1
		return ls