Skip to content

Valid Anagram

Problem

Given two strings, determine if one is an anagram of the other (same characters, same frequencies, different order allowed).

Approach

Counter comparison — count character frequencies for both strings and check equality.

When to Use

Equivalence class membership — same chars different order. Group anagrams builds on this. Also: frequency matching, permutation checking.

Complexity

Time O(n) with Counter, O(n log n) with sort
Space O(1) — bounded by alphabet size (26 for lowercase English)

Implementation

valid_anagram

Valid Anagram — check if two strings are anagrams of each other.

Problem

Given two strings, determine if one is an anagram of the other (same characters, same frequencies, different order allowed).

Approach

Counter comparison — count character frequencies for both strings and check equality.

When to use

Equivalence class membership — same chars different order. Group anagrams builds on this. Also: frequency matching, permutation checking.

Complexity

Time: O(n) with Counter, O(n log n) with sort Space: O(1) — bounded by alphabet size (26 for lowercase English)

is_anagram

is_anagram(s: str, t: str) -> bool

Return True if s and t are anagrams of each other.

is_anagram("anagram", "nagaram") True is_anagram("rat", "car") False

Source code in src/algo/strings/valid_anagram.py
def is_anagram(s: str, t: str) -> bool:
    """Return True if *s* and *t* are anagrams of each other.

    >>> is_anagram("anagram", "nagaram")
    True
    >>> is_anagram("rat", "car")
    False
    """
    return Counter(s) == Counter(t)
tests/strings/test_valid_anagram.py
"""Tests for the valid anagram problem."""

from hypothesis import given
from hypothesis import strategies as st

from algo.strings.valid_anagram import is_anagram


class TestIsAnagram:
    def test_basic_anagram(self) -> None:
        assert is_anagram("anagram", "nagaram")

    def test_not_anagram(self) -> None:
        assert not is_anagram("rat", "car")

    def test_empty_strings(self) -> None:
        assert is_anagram("", "")

    def test_different_lengths(self) -> None:
        assert not is_anagram("ab", "abc")

    def test_same_chars_different_counts(self) -> None:
        assert not is_anagram("aab", "abb")

    def test_single_char(self) -> None:
        assert is_anagram("a", "a")

    @given(s=st.text(alphabet="abcdef", min_size=0, max_size=30))
    def test_shuffled_string_is_anagram(self, s: str) -> None:
        # any permutation of s is an anagram of s
        import random

        shuffled = list(s)
        random.shuffle(shuffled)
        assert is_anagram(s, "".join(shuffled))

Implement it yourself

Run: just challenge strings valid_anagram

Then implement the functions to make all tests pass. Use just study strings for watch mode.

Reveal Solution

valid_anagram

Valid Anagram — check if two strings are anagrams of each other.

Problem

Given two strings, determine if one is an anagram of the other (same characters, same frequencies, different order allowed).

Approach

Counter comparison — count character frequencies for both strings and check equality.

When to use

Equivalence class membership — same chars different order. Group anagrams builds on this. Also: frequency matching, permutation checking.

Complexity

Time: O(n) with Counter, O(n log n) with sort Space: O(1) — bounded by alphabet size (26 for lowercase English)

is_anagram

is_anagram(s: str, t: str) -> bool

Return True if s and t are anagrams of each other.

is_anagram("anagram", "nagaram") True is_anagram("rat", "car") False

Source code in src/algo/strings/valid_anagram.py
def is_anagram(s: str, t: str) -> bool:
    """Return True if *s* and *t* are anagrams of each other.

    >>> is_anagram("anagram", "nagaram")
    True
    >>> is_anagram("rat", "car")
    False
    """
    return Counter(s) == Counter(t)