leetcode:290 Word Pattern-每日编程第三十九题
来源:互联网 发布:淘宝产品推广公司 编辑:程序博客网 时间:2024/05/18 09:51
Word Pattern
Total Accepted: 19405 Total Submissions: 71219 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.
class Solution {public: bool wordPattern(string pattern, string str) { int leftIndex=0; int rightIndex=0; int len=pattern.length(); int len1=str.length(); map<char,string> ma; map<char,string>::iterator it; map<string,char> ma1; map<string,char>::iterator it1; for(int i=0;i<len;i++){ while(rightIndex<len1&&str[rightIndex]!=' '){ rightIndex++; } if(rightIndex==leftIndex){ return false; } string temp=str.substr(leftIndex,rightIndex-leftIndex); if((it=ma.find(pattern[i]))!=ma.end()){ if(it->second!=temp){ return false; } }else{ ma[pattern[i]]=temp; } leftIndex=++rightIndex; if((it1=ma1.find(temp))!=ma1.end()){ if(it1->second!=pattern[i]){ return false; } }else{ ma1[temp]=pattern[i]; } } if(rightIndex<len1){ return false; } return true; }};
0 0
- leetcode:290 Word Pattern-每日编程第三十九题
- leetcode:58 Length of Last Word-每日编程第三十四题
- leetcode:160 Intersection of Two Linked Lists-每日编程第二十九题
- leetcode:190 Reverse Bits-每日编程第三十二题
- leetcode:223 Rectangle Area-每日编程第三十三题
- leetcode:20 Valid Parentheses-每日编程第三十七题
- leetcode:205 Isomorphic Strings-每日编程第三十八题
- 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
- 怎样学习大数据
- iOS——TabbarViewConttoller
- C++中的单例模式
- Python---常见问题
- 关系型数据库管理系统简介
- leetcode:290 Word Pattern-每日编程第三十九题
- hdu 2062
- 针对Faster RCNN具体细节以及源码的解读之SmoothL1Loss层
- SpringMVC与Struts2的对比
- iOS——之数组 字典 枚举
- 关于设置hidesBottomBarWhenPushed无效的问题
- 143Reorder List
- TCP聊天系统开发心得
- Java对象及其引用