Grokking 75: Top Coding Interview Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Word Pattern (easy)
On this page

Problem Statement

Examples

Try it yourself

Problem Statement

Given a pattern and a string s, return true if the string s follows the same pattern.

Here, the following the pattern means each character in the pattern should map to a single word in s, and each word in s should map to a single character in the pattern.

Examples

Example 1:

  • Input: pattern = "eegg", s = "dog dog cat cat"
  • Output: true
  • Explanation: The pattern "eegg" corresponds to the words "dog dog cat cat". Both 'e's map to "dog" and both 'g's map to "cat".

Example 2:

  • Input: pattern = "abca", s = "one two three four"
  • Output: false
  • Explanation: Here, a maps to the "one" and "four" both. So, the string doesn't follow the same pattern.

Example 3:

  • Input: pattern = "abacac", s = "dog cat dog mouse dog mouse"
  • Output: true
  • Explanation: The pattern "abacac" corresponds to the words "dog cat dog mouse dog mouse". 'a' maps to "dog", 'b' maps to "cat", and 'c' maps to "mouse".

Constraints:

  • 1 <= pattern.length <= 300
  • pattern contains only lower-case English letters.
  • 1 <= s.length <= 3000
  • s contains only lowercase English letters and spaces ' '.
  • s does not contain any leading or trailing spaces.
  • All the words in s are separated by a single space.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page

Problem Statement

Examples

Try it yourself