LeetCode 290:Word Pattern
2015-12-20 18:04
393 查看
Given a
find if
Here follow means a full match, such that there is a bijection between a letter in
a non-empty word in
Examples:
pattern =
pattern =
pattern =
pattern =
Notes:
You may assume
lowercase letters separated by a single space.
给定一个模式(pattern)和字符串(str),判断字符串是否遵循这个模式。
这意味着str与pattern是双向映射的,即一个str只对应一个pattern,一个pattern也只对应一个str。
例如:
pattern =
pattern =
pattern =
pattern =
由于我只会map,因此在建立双向映射的时候采取的办法是在每一个pattern后面加一个" "(即空格)来区分(以防止出现str为单个字母导致重复映射的情况)。大致思路是将str按空格分割放入vector<string>中,然后按顺序判断pattern与vector的每一对元素,若pattern或str出现过,那么判断对应映射是否正确;否则建立一对新的映射关系。
字符串分割算是自己勉勉强强写出来了,然而对于字符串的使用还是不够熟练。可以看到在判断循环中我先将pattern[i]来push_back给test,然后又append了一个空格进去。。。本来想直接用重载运算符+来直接写进去,但是好像并不能直接写的样子。
这里推荐一篇博客,对C++的字符串函数有非常非常详尽的解释,强烈推荐。另外,如果对于C++ STL有像这样介绍详细的博客(关键是要有清晰的成员函数列表及其对应函数参数,不需要使用实例),也请推荐给我,谢谢。
patternand a string
str,
find if
strfollows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in
patternand
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
patterncontains only lowercase letters, and
strcontains
lowercase letters separated by a single space.
给定一个模式(pattern)和字符串(str),判断字符串是否遵循这个模式。
这意味着str与pattern是双向映射的,即一个str只对应一个pattern,一个pattern也只对应一个str。
例如:
pattern =
"abba", str =
"dog cat cat dog"返回真。
pattern =
"abba", str =
"dog cat cat fish"返回假。(第一个a < - > dog,然而最后一个a < - > fish)
pattern =
"aaaa", str =
"dog cat cat dog"返回假。(第一个a < - >dog,然而第二个a < - > cat)
pattern =
"abba", str =
"dog dog dog dog"返回假。(第一个a < - > dog,然而第一个b也b < - > dog)
由于我只会map,因此在建立双向映射的时候采取的办法是在每一个pattern后面加一个" "(即空格)来区分(以防止出现str为单个字母导致重复映射的情况)。大致思路是将str按空格分割放入vector<string>中,然后按顺序判断pattern与vector的每一对元素,若pattern或str出现过,那么判断对应映射是否正确;否则建立一对新的映射关系。
字符串分割算是自己勉勉强强写出来了,然而对于字符串的使用还是不够熟练。可以看到在判断循环中我先将pattern[i]来push_back给test,然后又append了一个空格进去。。。本来想直接用重载运算符+来直接写进去,但是好像并不能直接写的样子。
class Solution { public: bool wordPattern(string pattern, string str) { map<string,string> mapl; vector<string> vec; str.append(" "); for(int start=0,count=0;start<str.length();count++) { if(str[start+count]==' ') { vec.push_back(str.substr(start,count)); start+=count+1; count=0; } } if(pattern.length()!=vec.size()) return false; for(int i=0;i<pattern.length();i++) { string test; test.push_back(pattern[i]); test.append(" "); if(mapl.count(test)||mapl.count(vec[i])) { if(mapl[test]!=vec[i]||mapl[vec[i]]!=test) return false; } else { mapl[test]=vec[i]; mapl[vec[i]]=test; } } return true; } };
这里推荐一篇博客,对C++的字符串函数有非常非常详尽的解释,强烈推荐。另外,如果对于C++ STL有像这样介绍详细的博客(关键是要有清晰的成员函数列表及其对应函数参数,不需要使用实例),也请推荐给我,谢谢。
相关文章推荐
- IOS面试算法题(4)——反斜对角线打印矩阵
- PHP 7 值得期待的新特性(下)
- 区别linq查询中的IEnumerable<T>和IQueryable<T>
- DataTransfer(setData()方法)
- 与拖放有关的事件
- 基于cuda的gpu加速
- IOS网络开发NSURLSession详解(一)概述
- listbox和comebox添加图片
- 【杭电】[2016]数据的交换输出
- ExtJs checkboxgroup选择事件
- PHP 7 值得期待的新特性(上)
- 实现拖放的步骤
- mysql层的内存分配
- hdoj 5115 Dire Wolf 【区间dp 暴力】
- acl3.1.4 跨平台网络通信与服务器编程框架发布了
- SDN
- 排序
- 拖放API
- LeetCode-11-Container With Most Water(证明)-Medium
- MongoDB(八)Mongodb——GridFS存储