Word Pattern
Given apattern
and a stringstr
, find ifstr
follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter inpattern
and a non-empty word instr
.
Example
Example 1:
Example 2:
Example 3:
Example 4:
Notes:
You may assumepattern
contains only lowercase letters, andstr
contains lowercase letters separated by a single space.
Note
建立mapping:key是character,value是word
set记录出现的word
必须是双向映射:
当key存在,mapping对应的单词不相同时,就false
当value存在,mapping对应的字符不存在,就false
Time:O(n) - pattern length
Space:O(n)
Code
Last updated