Check Anagrams
For a given pair of words check if they are anagrams. Print
YES if they are anagrams, or else print NO.Two words are anagrams if one word can be obtained by the rearrangement of the characters in another word.
INPUT: The first line of the input contains an integer
OUTPUT: The output should have the status of each of the
N pairs that are given in the input.All the statuses are separated by space.
Given pairs
dew wed
race care
In the word
dew, characters can be rearranged in the reverse order to get the word wed.So both the words are anagrams. The status of this pair is YES.
From the word race, when the characters r and c are swapped we will get the word care. So both the words are anagrams. The status of this pair is
YES.
Sample Input 1
2
dew wed
race care
Sample Output 1
YES YES
Sample Input 2
3
part trap
car air
some sum
Sample Output 2
YES NO NO
def anogram(a, b):
c = [0] * ord('z') + [0];
for i in a:
c[ord(i)] += 1;
for i in b:
c[ord(i)] -= 1;
for i in c:
if i != 0:
return False;
return True;
def main():
n = int(input())
for i in range(n):
a, b = input().split()
print('YES' if anogram(a, b) else 'NO', end=' ')
if __name__ == '__main__':
main()
Comments
Leave a comment