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 ...
随机推荐
- .Net Core 做请求监控NLog
使用 NLog 给 Asp.Net Core 做请求监控 https://www.cnblogs.com/cheesebar/p/9078207.html 为了减少由于单个请求挂掉而拖垮整站的情况发生 ...
- NET Core源代码通过Autofac实现依赖注入
查看.NET Core源代码通过Autofac实现依赖注入到Controller属性 阅读目录 一.前言 二.使用Autofac 三.最后 回到目录 一.前言 在之前的文章[ASP.NET Cor ...
- 054 Spiral Matrix 旋转打印矩阵
给出一个 m x n 的矩阵(m 行, n 列),请按照顺时针螺旋顺序返回元素.例如,给出以下矩阵:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]应该返回 [1,2, ...
- C# 实现Tree,包含parentId和children
1.先定义一个类型 public class Node { [JsonProperty(PropertyName = "id", NullValueHandling = NullV ...
- hbase按照时间戳删除记录
1.按照时间戳范围查询记录 echo "scan 'event_log', { COLUMN => 'cf:sid', TIMERANGE => [1466265600272, ...
- 《超实用的Node.js代码段》连载一:获取Buffer对象字节长度
我们知道Node.js框架下的Buffer对象能够对二进制数据提供很好的支持,那么获取一个Buffer对象真实的字节长度则是必须要用到的功能了.Node.js框架为开发人员提供了一个Buffer.by ...
- python的subprocess模块(写的不错留作查询)
python的subprocess模块 subprocess模块是python从2.4版本开始引入的模块.主要用来取代 一些旧的模块方法,如os.system.os.spawn*.os.popen*. ...
- Linux 安装Memcache扩展支持
查看相关软件包 yum search memcached 安装memcache yum -y install memcachedMemcache关联php yum -y install php-pec ...
- linux命令之文本查看
vi掌握练习: 英文文档,相同的单词复制粘贴光标移动编辑等操作: cat:显示文件所有内容,小文件查看时使用. 缺点:文件大时不方便查看,文件很大时,会抢占系统资源,会出现命令崩溃. [zyj@loc ...
- 编写C#程序,自动将bing首页图片设为壁纸
任务目标: 1,获取图片 2,设为壁纸 3,自动化 环境需求: .NET Framework 4.0+, Visual Studio 2017 ==================== 1,获取图片 ...