171. Word Pattern

EasyStringString

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

'Follows' means a full match: each letter in pattern maps bijectively to exactly one distinct word in s.

The input provides pattern and s separated by a comma; s consists of words separated by spaces.

Input: pattern (string of lowercase letters), comma, then sentence s (words separated by spaces).

Output: Return 'true' if s follows pattern, otherwise 'false'.

Examples

Example 1
Input: abba,dog cat cat dog
Output: true
Explanation: a→dog, b→cat; bijective mapping holds.
Example 2
Input: abba,dog cat cat fish
Output: false
Explanation: a maps to both 'dog' and 'fish'.
Example 3
Input: aaaa,dog cat cat dog
Output: false
Explanation: a maps to 'dog','cat','cat','dog' — not consistent.

Constraints

Asked by

BloombergGoogleMetaMicrosoftAmazon
Solve this problem in the editor →