Answer to Question #332239 in Python for sainath

Question #332239

Swap Competition




An English competition is being held where players have to play in groups of two. One of the players is given a word, and the second player has to choose a word such that, it is a rearrangement of the letters in the first player's word. They win if the above condition is satisfied. The number of groups is given by T.Write a programthat gives YES if they win and NO in the other case.




Note: All characters in the input will be in lower case.




Input




The first line of the input contains an integer T Each of the next T lines contains two words separated by space.




Output




The output should have the results of each of the T are given in the input.




groups that



All the results are separated by space.



Explanation




Given pairs




noon moon




part trap







Sample Input 1




2




noon moon part trap




Sample Output 1




NO YES




Sample Input 2




2




car rac




lock clock




Sample Output 2




YES, NO





1
Expert's answer
2022-04-22T04:43:51-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())
    answer = []
    for i in range(n):
        a, b = input().split()
        if anogram(a, b):
            answer.append('YES')
        else:
            answer.append('NO')
    print(answer)


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