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.
题目标签:Hash Table
题目给了我们一个pattern 和一个 str, 让我们判断,str 是否和 pattern 一致。
首先,把str split 到 words array 里,如果pattern 的长度 和 words 长度 不一样,直接返回false;
接下来利用HashMap,把pattern 里的 char 当作key, 每一个word 当作value 存入,如果遇到一样的char,但是有着不一样的value,返回false;如果遇到不一样的char,但有着一样的value,返回false;最后遍历完之后,返回true。
Java Solution:
Runtime beats 36.87%
完成日期:06/05/2017
关键词:HashMap
关键点:char 当作 key,word 当作 value
- class Solution
- {
- public boolean wordPattern(String pattern, String str)
- {
- HashMap<Character, String> map = new HashMap<>();
- String[] words = str.split(" ");
- if(pattern.length() != words.length)
- return false;
- for(int i=0; i<pattern.length(); i++)
- {
- char c = pattern.charAt(i);
- if(map.containsKey(c))
- {
- if(!map.get(c).equals(words[i]))
- return false;
- }
- else
- {
- if(map.containsValue(words[i]))
- return false;
- map.put(c, words[i]);
- }
- }
- return true;
- }
- }
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
LeetCode 290. Word Pattern (词语模式)的更多相关文章
- [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 单词模式
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] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- 290 Word Pattern 单词模式
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循这种模式.这里的 遵循 指完全匹配,例如在pattern里的每个字母和字符串 str 中的每个非空单词存在双向单映射关系 ...
- Leetcode 290 Word Pattern STL
Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...
- [leetcode] 290. Word Pattern (easy)
原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...
- LeetCode 290 Word Pattern
Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
随机推荐
- 【译】x86程序员手册26-7.5任务切换
7.5 Task Switching 任务切换 The 80386 switches execution to another task in any of four cases: 80386在以下四 ...
- Tcl之Math
expr is for Tcl to do math operations. It takes all of its arguments ("2 + 2" for example) ...
- Win10 “此环境变量太大。此对话框允许将值设置为最长2047个字符。" 解决方法。
打开注册表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment 双击右边的 Path (RE ...
- 让ios支持http协议
ios默认只支持https协议,打开info.plist文件,加入以下设置 NSAppTransportSecurity NSAllowsArbitraryLoads
- php利用array_filter()过滤数组空值
利用array_filter过滤数组空值 <?php $array = array( 0 => '霜天部落', 1 => false, 2 => 1, 3 => null ...
- HDU多校Round 6
Solved:2 rank:452 I. Werewolf 没有铁人 找铁狼 如果一个环中只有一条狼人边那个人就是铁狼 说铁狼是好人的人也是铁狼 #include <bits/stdc++.h& ...
- (转)MySQL中的索引详讲
序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于MySQL来说,知道了一些复杂的查询,就够了,但是 ...
- 继 S-HR之代码创建临时表并插入数据 完整功能之员工职业信息变更报表
目的示例1: 制作员工职业信息报表[S-HR系统的报表其实就是列表o.0,醉了] EcirrWithPP.js shr.defineClass("shr.custom.EcirrWithPP ...
- idea+maven配置log4j详解
经过上一篇的讲解,知道了实现log4j打印日志依赖的jar包共3个,在pom.xml中加入相关依赖: <!-- 添加log4j日志相关jar包:共3个jar--> <!-- http ...
- 利用python去调用shell命令时候的踩到的坑
shell中 True的返回值是0 False的返回值是1 Python中 True的返回值是1 False的返回值是0