Java [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.
解题思路:
使用HashMap来做。
代码如下:
public class Solution {
public boolean wordPattern(String pattern, String str) {
if(pattern == null || str == null)
return false;
String[] words = str.split(" ");
if(pattern.length() != words.length)
return false;
Map<Character, String> map = new HashMap<Character, String>();
for(int i = 0; i < pattern.length(); i++){
char t = pattern.charAt(i);
if(map.containsKey(t)){
if(!words[i].equals(map.get(t)))
return false;
} else {
if(map.containsValue(words[i]))
return false;
map.put(t, words[i]);
}
}
return true;
}
}
Java [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 、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 ...
- 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 ...
- [leetcode] 290. Word Pattern (easy)
原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...
随机推荐
- css3 怎么实现像书籍装订线的效果
.test{ width: 150px; height: 150px; margin: 20% auto; background-color: white; background-image: rep ...
- action间传多个参数时注意问题
通常我们action之间传参可以有多种形式,举例说明:示例1: <result name="test" type="redirect-action"> ...
- Introduction to Deep Neural Networks
Introduction to Deep Neural Networks Neural networks are a set of algorithms, modeled loosely after ...
- Windows Server 2008下共享资源访问走捷径 (不用用户名 和 密码 访问共享)
1. 启用来宾帐号2. 共享目录添加“Guest”帐号3. “gpedit.msc”,打开对应系统的组策略编辑窗口;在该编辑窗口的左侧显示区域,依次展开“本地计算机策略”/“计算机配置”/“Windo ...
- WP 类似扑克牌布局控件和类似扑克卡片控件
一.说明 本文代码来源: <windows phone 7 程序设计> Charles Petzold 控件效果: 二.要点: 1.ItemControl.子项容器模板(ItemsCont ...
- DiskGenius的 “终止位置参数溢出”错误解决方法。
(转帖)同事电脑系统启动突然明显变慢,重装系统后问题仍未解决(windowsxp sp3).帮忙分析感觉是磁盘分区表出现了错误,用通用PE工具箱进入PE系统,DiskGenius工具检查:“终止位置参 ...
- C#和Javascript中 正则表达式使用的总结
说明:本文并非原创,而是从网站上搜集了一些资料整理的!如有雷同,纯属巧合 1.js中正则表达式的使用 在js中定义正则表达式很简单,有两种方式,一种是通过构造函数,一种是通过//,也就是两个斜杠.例如 ...
- Unity打包APK横屏时的注意事项
由于你在Unity设置了横屏. 所以也需要在安卓的AndroidManifest.xml文件中, application/activity下声明为横屏.否则会黑屏,根本不给你报错,愁死你. 加上这一句 ...
- qt外部数据传入实现动态的折线图绘制
在嵌入式开发中,实现数据收集与显示很常见,对于希望数据稳定的应用来说, 折现图的表现形式很符合条件. 本实现是通过qt的signal-slot来 ...
- pancake sort的几个问题
1. 每次找剩下序列中的最大值,可以进行pancake sort,时间复杂度是O(n^2): 2. 求最少交换次数进行pancake sort的问题是个NP问题,搜索的时候,upper bound是2 ...