[LeetCode]290. Word Pattern
来源:互联网 发布:化工专业英语翻译软件 编辑:程序博客网 时间:2024/06/05 18:03
[LeetCode]290. Word Pattern
题目描述
思路
对str 分割字符串
然后字母对应的字符串存hash
不匹配返回false
若匹配,还需检测hash中的value是否有重复
代码
#include <iostream>#include <string>#include <vector>#include <unordered_map>using namespace std;class Solution {public: vector<string> split(string str, char ch) { vector<string> res; string temp = ""; for (char p : str) { if (p != ch) temp += p; else { res.push_back(temp); temp = ""; } } res.push_back(temp); return res; } bool wordPattern(string pattern, string str) { vector<string> strArr = split(str, ' '); if (pattern.size() != strArr.size()) return false; unordered_map<char, string> m; for (int i = 0; i < pattern.size(); ++i) { if (m.count(pattern[i])) { if (m[pattern[i]] != strArr[i]) return false; } else m[pattern[i]] = strArr[i]; } unordered_map<string, int> counts; for (auto p : m) { ++counts[p.second]; } return m.size() == counts.size(); }};int main() { string pattern = "abba", str = "aa aa aa aa"; Solution s; cout << s.wordPattern(pattern, str) << endl; system("pause"); return 0;}
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
- atomic 和 nonatomic 有什么区别?
- android学习(三) 保存文件
- 提供一个串口应用程序(使用的开发板是smart210)
- 严苛模式(StrictMode)
- 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
- [LeetCode]290. Word Pattern
- Android M 启动源码分析笔记之
- 备份MySQL数据库
- angularjs实现原生APP页面切换效果
- 代码篇——————一句话木马
- 注册建筑师取消【官方已公布】
- 银联支付:参数什么都填写正确,就是跳转的时候一点反应都没有
- Java利用PropertyDescriptor获取属性的 getter/setter 方法
- Quartz时钟表达式