LeetCode *** 290. Word Pattern
来源:互联网 发布:js把URL转换为对象 编辑:程序博客网 时间:2024/06/10 01:34
题目:
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 inpattern
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.
分析:
既然是判断两个值得模式是否一样,那么利用map的键/值来判断pattern与str的键/值是否对应相等即可。
代码:
class Solution {public: bool wordPattern(string pattern, string str) { map<string,int> mapS; map<char,int> mapP; int iP=0,iS=0; int lengthP=pattern.length(),lengthS=str.length(); while(iP<lengthP&&iS<lengthS){ string tmpS=""; while(str[iS]!=' '&&iS<lengthS){ tmpS+=str[iS]; iS++; } mapS[tmpS] ++; mapP[pattern[iP]] ++; if(mapS[tmpS]!=mapP[pattern[iP]])return false; iP++; iS++; } if(iS<lengthS||iP<lengthP) return false; else return true; }};
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
- __attribute__((regparm(n)))
- 互联网--评微软等公司数据结构和算法面试100题 .
- 计算理论初步:P vs NP 问题
- 正则表达式
- [iOS]UIView动画总结
- LeetCode *** 290. Word Pattern
- cscope插件,项目根目录、任意子目录下打开vim自动加载cscope.out
- 共同学习Java源码--常用数据类型--String(十三)
- 【GoogleCodeJam Qualification Round 2016】Problem A. Counting Sheep
- tomcat的war包手工部署和自动部署Windows8
- 提取多层嵌套JSON类型数据
- 转载:Java的异常处理机制:try、catch、finally、throw、throws
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
- dual表