Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Anagram Check for Two Strings

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Anagram Check for Two Strings. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Study needs to compare short learner responses for anagram-based exercises. Given two strings, determine whether they contain the same letters with the same frequencies, ignoring case, spaces, and non-alphanumeric characters.

Formal Specification

Implement is_anagram(s, t):

  1. Accept two strings, s and t.
  2. Consider only alphanumeric characters.
  3. Treat uppercase and lowercase characters as equal.
  4. Return True if the normalized strings contain exactly the same characters with identical frequencies. Otherwise, return False.

The input strings may contain spaces, punctuation, digits, and letters. An empty normalized string is valid, so two strings containing no alphanumeric characters are anagrams.

Constraints

  • 0 <= len(s), len(t) <= 10^5
  • Inputs contain printable ASCII characters
  • Comparisons are case-insensitive
  • Ignore spaces and non-alphanumeric characters
  • Do not sort the full strings

Function Signature

def is_anagram(s, t):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output