Answer to Question #207386 in Python for luifer

Question #207386

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

N. Each of the next N lines contains two words separated by space.

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.

Explanation

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


1
Expert's answer
2021-06-16T01:00:13-0400
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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS