Word Pattern – LeetCode 290
Problem
Description
Given a pattern and a string str, find if str 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 str.
Example
- pattern = “abba”, str = “dog cat cat dog” should return true.
- pattern = “abba”, str = “dog cat cat fish” should return false.
- pattern = “aaaa”, str = “dog cat cat dog” should return false.
- pattern = “abba”, str = “dog dog dog dog” should return false.
Answer
Original
Code
1 | class Solution { |
思路
简单的建立map去查表,但要注意反查的过程,时间复杂度$O(n)$,空间复杂度$O(n)$。
耗时$3$ ms,排名$66.67\%$
Better
思路
还没看到更好的思路。