Strings · Coding problem

Word Pattern

A free coding problem from the String Tokenization Reversal skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

A pattern of lowercase letters and a sentence s match when you can pair each letter with one word so that reading the pattern letter by letter spells out s: the same letter always stands for the same word, and different letters stand for different words. Return whether they match.

Input format

pattern
s

Example

Input:
abcab
red green blue red green
Output: true

a is red, b is green and c is blue. With red red blue red red, a and b would both be red, so that is false.

Constraints

  • 1 <= pattern.length <= 300
  • 1 <= s.length <= 3000
  • s is lowercase words separated by single spaces, with no leading or trailing space

How it's graded

  • Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in String Tokenization Reversal

Solve it Word Pattern free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime