290. Word Pattern
2016-07-07 11:59
330 查看
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.
跟205. Isomorphic Strings(重要)类似
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.
跟205. Isomorphic Strings(重要)类似
class Solution { public: bool wordPattern(string pattern, string str) { int lenP = pattern.size(); vector<string> vec; stringstream ss(str); string s; while (ss >> s){ vec.push_back(s); } int lenStr = vec.size(); if (lenP != lenStr){ return false; } unordered_map<string, string> hash, reflect; for (int i = 0; i < lenP; i++){ if (!hash.count(to_string(pattern[i])) && !reflect.count(vec[i])){ hash[to_string(pattern[i])] = vec[i]; reflect[vec[i]] = to_string(pattern[i]); } else{ if (hash[to_string(pattern[i])] != vec[i]){ return false; } } } return true; } };
相关文章推荐
- 5个现在就该使用的数组Array方法: indexOf/filter/forEach/map/reduce详解
- 原子核
- (C)结构数组
- register_nav_menus()(注册菜单)
- 学习随笔
- BootStrap 轮播插件(carousel)支持左右手势滑动的方法(三种)
- gcc include搜索路径和库文件搜索路径
- <iframe>标签自适应高度和宽度
- javah 生成header file 报错 问题解决
- (C)结构数组
- easyconf——基于AugularJS的配置管理系统开发框架
- 腾讯2016研发工程师编程题(2道)
- 【杭电oj】5101 - Select(STL & 二分)
- 深入浅出ES6(三):生成器 Generators
- SSIS用MDX 查询或多维数据库作为数据源报错的解决办法
- Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得
- 深入浅出ES6(三):生成器 Generators
- 很简洁的分页控件 适合MVC
- (C)位字段(bit-field)
- mac 安装tomcat8