标签:
题目链接:https://leetcode.com/problems/word-pattern/
题目:
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.
Examples:
"abba", str = "dog
cat cat dog" should return true."abba", str = "dog
cat cat fish" should return false."aaaa", str = "dog
cat cat dog" should return false."abba", str = "dog
dog dog dog" should return false.
Notes:
You may assume pattern contains only lowercase letters, and str contains
lowercase letters separated by a single space.
思路:
1、pattern字符相等的位置对应的字符串中的单词应该是相等的,pattern字符不相等的位置对应的字符串中单词应该也是不相等的。时间复杂度为
O(n^2),空间复杂度为O(1)。
2、用HashMap存储字符到单词的映射关系,时间复杂度为O(n),空间复杂度为O(n)。
算法1:
算法2:
标签:
原文地址:http://blog.csdn.net/yeqiuzs/article/details/51622679