[leetcode] 290. Word Pattern (easy)
思路:
建立两个哈希表,分别保存:
1 模式 :单词
2 单词 :是否出现过
水题
/**
* @param {string} pattern
* @param {string} str
* @return {boolean}
*/
var wordPattern = function(pattern, str) {
var pats = pattern.split('');
var words = str.split(' ');
if (pats.length !== words.length) return false;
var map = {}; // p : word
var wordMap = {}; // word : bool
for (let i = 0; i < pats.length; i++) {
var p = pats[i];
var s = words[i];
if (!wordMap[s] && !map[p]) {
map[p] = s;
wordMap[s] = true;
} else if (map[p] !== s) {
return false;
}
}
return true;
};
[leetcode] 290. Word Pattern (easy)的更多相关文章
- [LeetCode] 290. Word Pattern 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- [LeetCode] 290. Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- LeetCode 290. Word Pattern (词语模式)
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode 290 Word Pattern
Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- Leetcode 290 Word Pattern STL
Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...
- Java [Leetcode 290]Word Pattern
题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- leetcode 290 Word Pattern(map的应用)
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
随机推荐
- configure -help
http://www.360doc.com/content/14/1215/17/18578054_433158382.shtml http://blog.csdn.net/mociml/articl ...
- 利用js参数,保持客户端文件的新鲜度
不知道你是否碰到过如下情况,在服务端更新了一个重要的js文件后,由于浏览器的缓存机制,导致用户始终不能获取到最新的文件,此时的你恨不得有孙悟空吹毛化身的法术,帮用户清除浏览器的缓存.缓存既是程序员的好 ...
- delphi android 录像(使用了JMediaRecorder,MediaRecorder的使用方法可参考网上java的相关说明)
delphi xe系列自带的控件都无法保存录像,经网友帮忙,昨天终于实现了录像功能(但有个问题是录像时无画面显示),程序主要使用了JMediaRecorder,MediaRecorder的使用方法可参 ...
- 如何在 cmd 命令行中查看、修改、删除与添加环境变量
Windows 和 linux 区别 一.查看所有环境变量的名称和值:Linux下:exportWindows下:set二.根据名称查该环境变量的值:Linux下:echo $环境变量名比如:echo ...
- python字典的内建函数
In [70]: test=dict(x=1,y=2,z=3) In [71]: test Out[71]: {'x': 1, 'y': 2, 'z': 3} In [72]: a=['a','b', ...
- C# Winfrom 简单的运用Timer控件
注意,在使用DateAndTime时,需要添加引用 using Microsoft.VisualBasic;否则不可以计算时间之间的差值. using System; using System.Col ...
- Spring Framework 组件注册 之 @Import
Spring Framework 组件注册 之 @Import 写在前面 向spring中注册组件或者叫javaBean是使用spring的功能的前提条件.而且spring也提供了很多种方式,让我们可 ...
- spring boot + druid + mybatis + atomikos 多数据源配置 并支持分布式事务
文章目录 一.综述 1.1 项目说明 1.2 项目结构 二.配置多数据源并支持分布式事务 2.1 导入基本依赖 2.2 在yml中配置多数据源信息 2.3 进行多数据源的配置 三.整合结果测试 3.1 ...
- python基础--定义装饰器(内置装饰器)
装饰器的定义: 装饰器本质上就是一个python函数,它可以让其它函数在不需要做任何代码改动的前提下增加额外的功能,装饰器的返回值也是一个函数对象.它经常用于有切面需求的场景中,比如-- >插入 ...
- 【设计模式】结构型02装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 意图:动态地给一个对象添加一些额外的职责.就增加功能来说,装饰器模式相比生成子类更为灵活. 主要解决:一般的,我们为了扩展一个类经常使用继承方式实现,由 ...