290. Word Pattern
题目大意
Given a pattern and a string s
, find if s
follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern
and a non-empty word in s
.
中文释义
给定一个模式 pattern
和一个字符串 s
,判断 s
是否遵循相同的模式。
这里的遵循意味着完全匹配,即 pattern
中的每个字母和 s
中的非空单词之间存在一一对应的关系。
示例
Example 1:
Input: pattern = "abba"
, s = "dog cat cat dog"
Output: true
Example 2:
Input: pattern = "abba"
, s = "dog cat cat fish"
Output: false
Example 3:
Input: pattern = "aaaa"
, s = "dog cat cat dog"
Output: false
限制条件
1 <= pattern.length <= 300
pattern
只包含小写英文字母。1 <= s.length <= 3000
s
只包含小写英文字母和空格' '
。s
不包含任何前导或尾随空格。s
中的所有单词由单个空格分隔。
解题思路
方法
这个解决方案旨在判断字符串 s
是否按照给定的 pattern
模式进行排列。主要方法是使用两个哈希表来确保