Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Check Anagrams with Frequency ArraysE02. Find the first non-repeating characterE03. Count pattern occurrences with a sliding windowE

Problem No 1

Check Anagrams with Frequency Arrays

Easy

8 minute session

Summary

Determine whether two strings are anagrams by comparing character counts.

Problem statement

Given two strings s and t, return True if t is an anagram of s. Consider all characters exactly as given (case-sensitive). Spaces and punctuation count as characters. Return False otherwise. Both inputs may be empty strings. Time complexity should be O(n).

Task

Implement a function that returns True if two input strings contain the exact same characters with the same frequencies (case-sensitive, includes spaces and punctuation).

Examples

Basic anagram

Input

s = "listen", t = "silent"

Output

True

Explanation

Both strings contain the same characters with the same counts.

Input format

Two string arguments: is_anagram(s, t)

Output format

Return a boolean: True if t is an anagram of s, otherwise False.

Constraints

0 <= len(s), len(t) <= 10^5. Use O(n) time and O(k) additional space where k is alphabet size.

Samples

Sample input 0

is_anagram("triangle", "integral")

Sample output 0

True

Explanation 0

Same characters and counts.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

09:15 AM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.