Knowee
Questions
Features
Study Tools

Given 2 strings, check if they are anagrams. An anagram is a rearrangement of the letters of one word to form another word. In other words, some permutations of string A must be the same as string B.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line containing 2 space-separated strings.Output FormatFor each test case, print True/False, separated by a new line.Constraints10 points1 <= T <= 1001 <= len(S) <= 103'a' <= S[i] <= 'z'40 points1 <= T <= 1001 <= len(S) <= 105'a' <= S[i] <= 'z'ExampleInput4iamlordvoldemort tommarvoloriddleb hstop posthi heyOutputTrueFalseTrueFalse

Question

Given 2 strings, check if they are anagrams. An anagram is a rearrangement of the letters of one word to form another word. In other words, some permutations of string A must be the same as string B.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line containing 2 space-separated strings.Output FormatFor each test case, print True/False, separated by a new line.Constraints10 points1 <= T <= 1001 <= len(S) <= 103'a' <= S[i] <= 'z'40 points1 <= T <= 1001 <= len(S) <= 105'a' <= S[i] <= 'z'ExampleInput4iamlordvoldemort tommarvoloriddleb hstop posthi heyOutputTrueFalseTrueFalse

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python solution for the problem:

def check_anagram(str1, str2):
    return sorted(str1) == sorted(str2)

T = int(input())
for _ in range(T):
    str1, str2 = input().split()
    print(check_anagram(str1, str2))

This solution works by first defining a function check_anagram that takes two strings as input. It sorts the characters in each string and checks if they are equal. If they are equal, it means the strings are anagrams of each other, so it returns True. Otherwise, it returns False.

Then it reads the number of test cases T. For each test case, it reads the two strings, checks if they are anagrams using the check_anagram function, and prints the result.

This problem has been solved

Similar Questions

Write a program to check two strings are anagram or not.

You are given N strings of length M, count the number of anagramic groups. An anagramic group is a list of strings which are anagrams of each other.Input FormatThe first line of input contains T - the number of test cases. In each test case, the first line contains N - the number of strings and M - the length of each string, separated by a space. The N subsequent lines each contain a string of length M.Output FormatFor each test case, print the count of the number of anagramic groups in the given N strings, separated by a new line.Constraints1 <= T <= 1001 <= N <= 1001 <= M <= 1000'a' <= str[i] <= 'z'ExampleInput25 4artsrankstarrantrats9 5teslastartslatesaltetartsasterarsonastlenorseOutput35ExplanationTest-Case 1The strings "arts", "star" and "rats" can be grouped together because they are anagrams of each other.The total number of such groups is 3 and the groups are {"arts", "rats", "star"}, {"rant"} and {"rank"}.

Converting AnagramsMax Score: 100Given two strings A and B, find the minimum number of characters that need to be deleted from these 2 strings to make them anagrams of each other.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line contains 2 space separated strings - A and B.Output FormatFor each test case, print the minimum number of deletions, separated by a new line.Constraints1 <= T <= 10001 <= len(A), len(B) <= 1000'a' <= A[i], B[i] <= 'z'ExampleInput2smart interviewsdata structuresOutput912ExplanationSelf Explanatory

Given a string S. the task is to determine if there exists another string that is an anagram of S.An anagram is a word or phrase formed by rearranging the letters of another word or phrase. For example "seekg" is an anagram of "geeks".Example 1:Input: S = "ab"Output: YESExplanation:The string "ba" is not identical to S and is an anagram of S

Write a program that figures out whether one string is an anagram of another string. The program should ignore white space and punctuation

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.