Count case-insensitive word frequencies in a string

Quick Overview

Count how many times each word appears in a string, where a word is a maximal run of ASCII letters and matching ignores case. It tests single-pass string scanning, hash map counting, and careful handling of separators, digits and the final word.

Count case-insensitive word frequencies in a string

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `text`, return a mapping from each word to the number of times it appears in `text`. A word is a maximal sequence of alphabetic characters (`a-z`, `A-Z`). The comparison is case-insensitive: all words are normalized to lowercase in the result. Any non-alphabetic characters (spaces, digits, punctuation) act as separators and are ignored. If `text` contains no words, return an empty mapping. ### Function Signature ```python def word_frequency(text: str) -> dict[str, int]: ``` ### Rules - Only the ASCII letters `a-z` and `A-Z` form words. Every other character, digits included, splits words apart, so `"World123world"` holds two words. - Keys in the returned mapping are lowercase words, and each value is that word's count (at least 1). Key order does not matter: the result is compared as a set of word and count pairs. ### Constraints - `0 <= len(text) <= 10^5` - `text` consists of printable ASCII characters. ### Examples ```text Input: text = "The quick brown fox jumps over the lazy dog. The dog barks!" Output: {"the": 3, "quick": 1, "brown": 1, "fox": 1, "jumps": 1, "over": 1, "lazy": 1, "dog": 2, "barks": 1} ``` Words are matched case-insensitively, so "The", "the" and the second "The" all count toward `"the"`, which appears 3 times. `"dog"` appears twice and every other word appears once. ```text Input: text = "Hello, hello, HELLO!!! World123world." Output: {"hello": 3, "world": 2} ``` Commas, spaces and exclamation marks separate the three spellings of "hello", which all become `"hello"`. The digits in "World123world" split it into two occurrences of `"world"`. ```text Input: text = "42 + 7 = 49?" Output: {} ``` The text has no letters, so it has no words and the result is an empty mapping.

Overview: Count how many times each word appears in a string, where a word is a maximal run of ASCII letters and matching ignores case. It tests single-pass string scanning, hash map counting, and careful handling of separators, digits and the final word.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string text, return a mapping from each word to the number of times it appears in text.

A word is a maximal sequence of alphabetic characters (a-z, A-Z). The comparison is case-insensitive: all words are normalized to lowercase in the result.

Any non-alphabetic characters (spaces, digits, punctuation) act as separators and are ignored. If text contains no words, return an empty mapping.

Function Signature

def word_frequency(text: str) -> dict[str, int]:

Rules

  • Only the ASCII letters a-z and A-Z form words. Every other character, digits included, splits words apart, so "World123world" holds two words.
  • Keys in the returned mapping are lowercase words, and each value is that word's count (at least 1). Key order does not matter: the result is compared as a set of word and count pairs.

Constraints

  • 0 <= len(text) <= 10^5
  • text consists of printable ASCII characters.

Examples

Input: text = "The quick brown fox jumps over the lazy dog. The dog barks!"
Output: {"the": 3, "quick": 1, "brown": 1, "fox": 1, "jumps": 1, "over": 1, "lazy": 1, "dog": 2, "barks": 1}

Words are matched case-insensitively, so "The", "the" and the second "The" all count toward "the", which appears 3 times. "dog" appears twice and every other word appears once.

Input: text = "Hello, hello, HELLO!!! World123world."
Output: {"hello": 3, "world": 2}

Commas, spaces and exclamation marks separate the three spellings of "hello", which all become "hello". The digits in "World123world" split it into two occurrences of "world".

Input: text = "42 + 7 = 49?"
Output: {}

The text has no letters, so it has no words and the result is an empty mapping.