Skip to content

Word Pattern

EasyArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.

Given a pattern of letters and a string s of words separated by single spaces, return true if s follows the pattern: there is a one-to-one match between letters and words, so each letter always stands for the same word and no two letters stand for the same word.

pattern = "abba", s = "dog cat cat dog"    →  true
pattern = "abba", s = "dog cat cat fish"   →  false
pattern = "abba", s = "dog dog dog dog"    →  false  (a and b both mean dog)

Constraints

  • 1 <= pattern.length <= 300
  • pattern contains only lowercase English letters.
  • 1 <= s.length <= 3000
  • s contains lowercase letters and single spaces, with no leading or trailing space.