Sunday, 26 June 2016

Anagram Program in Python

 Python Program to check if words are anagram 

Python Program for anagram


A program that takes the number of test cases to be check for anagram and than takes the input pair of strings as a comma separated values and print YES if they are anagrams else prints NO.

Anagram - a word, phrase, or sentence formed from another by rearranging its letters: “Angel” is an anagram of “glean”

Example Input:
3
xyz yzx
abab aaba
you me

Expected Output:
YES
NO
NO

#Python Program to check anagrams

# Input <- number of test cases
num_of_test_cases = int(raw_input("Enter the number of Test cases: "))

words = []

# Input <- Words to be tested
for i in range(num_of_test_cases):
    text1, text2 = raw_input("enter two string with space:").split(" ")
    words.append([text1,text2])

# Function to check for anagram
def check_anagram():
    for i in range(num_of_test_cases):
        if(sorted(words[i][0]) == sorted(words[i][1])):
            print("YES")
        else:
            print("NO")
           
# Call anagram function to check
check_anagram()

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel