LeetCode(290) Word Pattern
题目
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.
分析
字符串的模式匹配,类似于离散数学中的双向映射问题。
利用unordered_map数据结构建立双向映射,逐个判断匹配。
AC代码
class Solution {
public:
bool wordPattern(string pattern, string str) {
if (str.empty())
return false;
//从输入的字符串源串得到字符串数组
vector<string> strs;
string s = "";
for (int i = 0; str[i] != '\0'; ++i)
{
if (str[i] == ' ')
{
strs.push_back(s);
s = "";
}
else
s += str[i];
}//for
//保存末尾单词
strs.push_back(s);
if (pattern.size() != strs.size())
return false;
//判断模式匹配
int len = pattern.size();
unordered_map<char, string> um1;
unordered_map<string, char> um2;
for (int i = 0; i < len; ++i)
{
auto iter1 = um1.find(pattern[i]);
auto iter2 = um2.find(strs[i]);
if (iter1 != um1.end() && iter2 != um2.end())
{
if ((*iter1).second == strs[i] && (*iter2).second == pattern[i])
continue;
else
return false;
}//if
else if (iter1 == um1.end() && iter2 != um2.end())
return false;
else if (iter1 != um1.end() && iter2 == um2.end())
return false;
else
um1.insert({ pattern[i], strs[i] });
um2.insert({ strs[i], pattern[i] });
}//for
return true;
}
};
LeetCode(290) Word Pattern的更多相关文章
- LeetCode(79) Word Search
题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- Entitas实现简析
Entitas实现简析 这里主要讲Entitas的执行原理,不讲Entitas的代码生成方面. ECS简介 ECS(实体-组件-系统)是一种常用于游戏开发的架构模式. 实体: 实体只是一个 ...
- .net C#实现 中文转Unicode、Unicode转中文 及与js对应关系
中文转Unicode:HttpUtility.UrlEncodeUnicode(string str); 转换后中文格式:"%uxxxx" 举例:"柳_abc123&q ...
- springcloud-Netflix创建服务消费者
目录 springcloud-Netflix创建服务消费者 Ribbon 创建服务消费者-Ribbon方式 ribbon的架构 Feign 创建包和基本项目结构 创建Feign访问服务的接口和访问co ...
- SQL2000,2005,2008安装在一台机子上
工欲善其事,必先利其器.本机的系统是在网上自己下载的,是32位windows7旗舰版.因为学习,需要在一台机子上同时安装SQL Server2000,2005,2008三个版本的数据库.先是在网上查了 ...
- STM8 PIN setting(output)
今日在设置引脚输出的时候,本想设置为open-drain输出,然后对其输出高低.但是发现无法输出高(初始化为开漏低电平),始终为低.后来改为push-pull 输出,就能输出高低了.真有意思,转到SP ...
- linux配置tomcat已service方式启动
1. 在/etc/init.d目录下新建文件,命名为tomcat2. 对tomcat文件进行编辑,执行 # cd /etc/init.d/ # vi tomcat 将下面代码粘上去 注意:下面代码ja ...
- ios 12 xcode10 新升级的编译报错libstdc++.6.0.9 Multiple commands produce
问题一 编译报错 Showing Recent Messages :-1: Multiple commands produce '/Users/duning/Library/Developer/Xco ...
- elasticsearch查询方式
1.query string a).GET /index/type/_search ===>>查询所有 b).GET /index/type/_search?q=filed:value&a ...
- Web端 年月日下拉表 密码判断 按钮判断是否提交
生日: <asp:DropDownList ID="selYear" runat="server"></asp:DropDownList> ...
- 【TensorFlow入门完全指南】模型篇·最近邻模型
最近邻模型,更为常见的是k-最近邻模型,是一种常见的机器学习模型,原理如下: KNN算法的前提是存在一个样本的数据集,每一个样本都有自己的标签,表明自己的类型.现在有一个新的未知的数据,需要判断它的类 ...