14 · Hashing & Prefix Sums

#290 Word Pattern

Easysolve on LeetCode ↗

frequently reported atBloombergGoogleAmazon

the problem

In plain words

pattern is a string of letters and s is a sentence of words separated by single spaces. Return true if s follows pattern: letters and words pair up one-to-one, so each letter always stands for the same word and no two letters stand for the same word.

in pattern = "abba", s = "dog cat cat dog"

out true

in pattern = "abba", s = "dog cat cat fish"

out false

in pattern = "aaaa", s = "dog cat cat dog"

out false

  • 1 ≤ pattern.length ≤ 300, lowercase letters
  • 1 ≤ s.length ≤ 3000, lowercase words separated by single spaces (no leading or trailing spaces)

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #205
      Isomorphic Strings
      Easyin Hashing
    2. #49
      Group Anagrams
      Mediumin Arrays & Strings