LeetCode Oj 290. Word Pattern
来源:互联网 发布:驾驶证ps软件 编辑:程序博客网 时间:2024/06/06 04:21
290. Word Pattern
- Total Accepted: 43861
- Total Submissions: 144885
- Difficulty: Easy
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.
Credits:
class Solution {public: bool wordPattern(string pattern, string str) { int vis[500]; memset(vis , 0, sizeof(vis)); map<int,string>mp; map<string,int>b_mp; vector<int>a; vector<string>b; for(int i=0;i<pattern.length();i++) a.push_back(pattern[i]); string t=""; str=str+" "; for(int i=0;i<str.length();i++) if(str[i]==' ') { b.push_back(t); t=""; } else { t=t+str[i]; } if(a.size()!=b.size()) return false; for(int i=0;i<a.size();i++) { if(!vis[a[i]]) { if(b_mp[b[i]]) return false; vis[a[i]]=1; mp[a[i]]=b[i]; vis[a[i]]=1; b_mp[b[i]]=a[i]; } else { if(mp[a[i]]!=b[i]) return false; } } return true; }};
0 0
- <LeetCode OJ> 290. Word Pattern
- LeetCode Oj 290. Word Pattern
- 【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
- 漫步微积分三——如何计算切线的斜率
- 深入了解iOS中的VC切换的传值方式
- HOW TO RUN YOUR WEB SERVER AND MQTT WEBSOCKETS BROKER ON THE SAME PORT
- centos lvs+keepalived+mysql实现mysql数据库热备主主复制
- ebs Start Page
- LeetCode Oj 290. Word Pattern
- 【jzoj4603】【颜料大乱斗】【线段树】
- L2-003. 月饼
- Android工具类之创建删除快捷图标
- 基本数据类型总结
- opencv-载入一幅图像并进行平滑处理
- HOW TO RUN YOUR WEB SERVER AND MQTT WEBSOCKETS BROKER ON THE SAME PORT
- 基本设计模式:单例模式和工厂模式代码实现
- android SQL 模糊查询