⌘K
Flycodx

Valid Anagram

Valid Anagram

EasyStrings28% accepted

Two strings are anagrams if one can be formed by rearranging all the characters of the other. Given two strings s and t, determine whether t is an anagram of s. Your solve(...) function receives these inputs directly as ready-made arguments — no input reading or parsing needed. Just compute and return the answer. Example: s = "anagram", t = "nagaram" → YES Example: s = "rat", t = "car" → NO

Input format — only if your code reads from stdin

Input format (stdin): - First line: string s - Second line: string t Output: YES if t is an anagram of s, otherwise NO.

Approach

If the lengths differ, immediately print NO. Otherwise count character frequencies for both strings — if all counts match, they are anagrams. A single frequency array of size 26 (increment for s, decrement for t) works in O(n) time.

Input

anagram nagaram

Output

YES
  • •1 ≤ len(s), len(t) ≤ 5×10^4, strings contain only lowercase English letters
solution.py
Loading editor...
Test 1
Test 2
Test 3

Press Run to test your custom input, or Submit to judge against all test cases.

Sign in to continue

Create a free account to run code, submit solutions and track progress.

  • Free forever — no credit card
  • 10+ languages with live execution
  • Points, streaks & leaderboards
Auto Save OnLn 1, Col 1Spaces: 4UTF-8LFPython 3