margana-is-an-anagram

Each army fighting the world war Z is denoted with a word consisting any combination of 26 alphabets. An army is a match for his/her opponent if and only if his name is an anagram of his/her opponent. Your job as a programmer is to help find whether two randomly selected members are match for each other or not.

(An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once. Your task is to find whether a given pair of words is anagrams to each other.
 Eg: Silent is anagram to listen.)

Input:
Number of test cases (t ≤ 10).  Each test case consists of a pair of strings (length of string ≤ 1000)  separated by a single space.

Output:
For each test case, print ‘Yes’ if they are a match to each other and ‘No’ otherwise.

SAMPLE INPUT
2
listen silent
mancap pacmaan

SAMPLE OUTPUT
Yes
No

Source: hackerearth | Difficulty: MEDIUM


Solutions:
t=int(input())
for k in range(t):
	l=list(input().split(" "))
	one=list(l[0])
	two=list(l[1])
	one.sort()
	two.sort()
	if(one==two):
		print("Yes")
	else:
		print("No")
		


#assuming lowercase alphabets are present in strings and both are of same lengths

def anagram_linear_time(s1,s2):
	c1=[0]*26
	c2=[0]*26
	for i in range(len(s1)):
		pos=ord(s1[i])-ord('a')
		c1[pos]+=1
	for j in range(len(s2)):
		pos=ord(s2[j])-ord('a')
		c2[pos]+=1
	k=0
	stillOk=True
	while k<26 and stillOk:
		if(c1[k]==c2[k]):
			k+=1
		else:
			stillOk=False
	return stillOk

t=eval(input())
while t:
	res=False
	t=t-1
	s1,s2=list(map(str,input().split()))
	s1=s1.lower()
	s2=s2.lower()
	if(len(s1)!=len(s2)):
		res=False
	else:
		res=anagram_linear_time(s1,s2)
	if res:
		print("Yes")
	else:
		print("No")
		
	


#assuming lowercase alphabets are present in strings and both are of same lengths

def anagram_linear_time(s1,s2):
	c1=[0]*26
	c2=[0]*26
	for i in range(len(s1)):
		pos=ord(s1[i])-ord('a')
		c1[pos]+=1
	for j in range(len(s2)):
		pos=ord(s2[j])-ord('a')
		c2[pos]+=1
	k=0
	stillOk=True
	while k<26 and stillOk:
		if(c1[k]==c2[k]):
			k+=1
		else:
			stillOk=False
	return stillOk

t=eval(input())
while t:
	res=False
	t=t-1
	s1,s2=list(map(str,input().split()))
	s1=s1.lower()
	s2=s2.lower()
	if(len(s1)!=len(s2)):
		res=False
	else:
		res=anagram_linear_time(s1,s2)
	if res:
		print("Yes")
	else:
		print("No")