easyHash TableArrays & Hashing

Word Pattern

easyTime: O(n)Space: O(n)

Recognize the pattern

check if string follows word-to-pattern mappingbijection between words and pattern charssame as isomorphic strings

Brute force idea

Try all mappings — O(n!).

Better approach

Two maps: pattern char → word AND word → pattern char. Check consistency at each position. O(n × m) where m is average word length.

Key invariant

Same as isomorphic strings but with characters and words. The mapping must be bijective — each pattern char maps to exactly one word and vice versa.

Watch out for

Only checking one direction — 'a b' matching 'cc cc' requires both directions to detect the inconsistency.

Arrays & Hashing Pattern