Educatifu
Open menu

Valid Anagram

Mediumstringshashing

Two strings are anagrams if one is a rearrangement of the other — the same letters, each used the same number of times.

Input

Two lines: the string s on the first line and the string t on the second.

Output

true if t is an anagram of s, otherwise false.

Examples

Example 1

Input
listen
silent
Output
true

silent is a rearrangement of listen.

Example 2

Input
abc
abd
Output
false

Different letters (c vs d), so not an anagram.

Example 3

Input
aabb
bbaa
Output
true

Same two letters, twice each.

Constraints

  • 1 <= length of each string <= 10^4
  • Both strings consist of lower-case English letters only.

Hints

Reveal hint 1

Two strings are anagrams exactly when they have the same characters with the same frequencies.

Reveal hint 2

Sorting both strings and comparing them is a simple correct approach; counting letters is a faster one.

Reference solution (spoiler — try it yourself first)

Approach

Anagrams have identical character multisets. Sort both strings (or compare letter counts) and check equality.

import sys
lines = sys.stdin.read().split('\n')
a = lines[0] if len(lines) > 0 else ''
b = lines[1] if len(lines) > 1 else ''
print("true" if sorted(a) == sorted(b) else "false")

← All practice problems

Bring us the problem, not a perfect specification

Tell us what needs to change, who it affects and any important deadline. We will review the context and reply with useful next questions.

  1. 01Share contextDescribe the workflow, constraint or risk.
  2. 02Clarify togetherWe identify missing facts and useful options.
  3. 03Choose a startAgree a focused assessment or delivery step.
Start a conversation