[leetcode] 290. Word Pattern
来源:互联网 发布:信用卡账单修改软件 编辑:程序博客网 时间:2024/06/05 00:17
Question:
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: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.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
Solution:
建立两个map,相当于建立一个双向映射,对每一个pattern建立到一个单词的映射,同时建立一个反映射,然后判断后续的已出现的pattern是否对应正确的单词就好。注意pattern比单词多或单词比pattern多的情况就好。
AC后看了一眼别人的代码,思路是类似的,不过不用建立第二个映射,使用set记录已出现的单词就好,还有获得单词直接使用索引,不用每次都把string的一部分去掉,节省时间。
时间复杂度:O(n)
空间复杂度:O(n)
class Solution {public: bool wordPattern(string pattern, string str) { map<char, string> m1; map<string, char> m2; for (int i = 0; i < pattern.size(); i++) { string tmp = getNext(str); if (tmp == "") return false; if (m1.find(pattern[i]) == m1.end()) { if (m2.find(tmp) == m2.end()) { m1[pattern[i]] = tmp; m2[tmp] = pattern[i]; } else { cout << "1 " << tmp; return false; } } else if (m1[pattern[i]] != tmp) { return false; } } if (str.empty()) return true; return false; } string getNext(string & str) { string ret; for (int i = 0; i < str.size(); i++) { if (str[i] == ' ') { str = str.substr(i+1); return ret; } ret += str[i]; } ret = str; str = ""; return ret; }};
阅读全文
0 0
- 【leetcode】290. Word Pattern
- 【leetcode】290. Word Pattern
- 290. Word Pattern LeetCode
- Leetcode 290. Word Pattern
- 【LeetCode】290. Word Pattern
- LeetCode-290. Word Pattern
- LeetCode *** 290. Word Pattern
- leetcode 290. Word Pattern
- LeetCode 290. Word Pattern
- LeetCode 290. Word Pattern
- [leetcode] 290. Word Pattern
- 【LeetCode】290. Word Pattern
- LeetCode-290.Word Pattern
- Leetcode 290. Word Pattern
- leetcode:290. Word Pattern
- 290.[Leetcode]Word Pattern
- [Leetcode]290. Word Pattern
- LeetCode 290. Word Pattern
- 写一个函数求平均数
- 注入指针修改值
- spring中 mvc 的重定向与请求转发
- 1.应用统计与随机过程第二章部分课上例题
- Passwords
- [leetcode] 290. Word Pattern
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)
- Codeforces 876 B Divisiblity of Differences
- 笔试题记录
- 557. Reverse Words in a String III
- hdu1219 AC Me
- 2017年10月17日-ACM涉及知识需掌握
- SpringBoot自定义静态资源映射方法
- swift脚本编程:一键生成AppIcon