livepython.net

198. Python Program to Check If Two Words Are Anagrams

EasyPhase 1 · Warm-up

Here you need lower(), sorted() and one if. You have met all three.

Overview

The anagram check is one of the most common interview questions about strings. Word games and puzzles use it too.

Problem Statement

Yuki plays a word game with friends. Two words are anagrams when they use the same letters, the same number of times, in any order. For example, listen and silent are anagrams.

Capital letters do not matter: Night and thing are anagrams too.

Read two words. Print <first> and <second> are anagrams or <first> and <second> are not anagrams. Print the words just as they were typed.

Steps

  1. Read the first word, then the second word.
  2. Make lowercase copies of both words, so capital letters do not matter.
  3. Put the letters of each copy in A to Z order.
  4. If the two sorted lists are the same, the words are anagrams. Print the right line.

Examples

Example 1. Input:

listen
silent

gives:

listen and silent are anagrams

Why: both words have the letters e, i, l, n, s and t, one of each.

Example 2. Input:

apple
paper

gives:

apple and paper are not anagrams

Why: apple has an l, but paper has an r instead.

Input

Two lines, with one word on each line. A word has letters only (1 letter or more). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

One line: <first> and <second> are anagrams or <first> and <second> are not anagrams. The tests try 6 different inputs, also words with capital letters.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 12 · Working With Strings
solution.py
# write your code here

EXPECTED OUTPUT

1listen and silent are anagrams

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.