Search⌘ K
AI Features

Solution: Word Pattern

Understand how to verify if a string follows a given pattern by implementing a one-to-one mapping between pattern characters and words using hash maps. Learn to break down the problem, handle constraints, and ensure consistent mapping through an efficient algorithm suitable for coding interviews.

Statement

You are given a pattern and a string, s. You need to determine whether the string s follows the same pattern.

A string s is said to follow a pattern if a bijection exists between a character in the pattern and a non-empty word in s.

  • Each character in the pattern must map to exactly one unique word in s.

  • Each word in s must map to exactly one unique character in pattern.

  • No two characters map to the same word, and no two words map to the same character.

Constraints:

  • 11 \leq pattern.length 100\leq 100 ...