Word Pattern
package cn.edu.xidian.sselab.hashtable;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author zhiyong wang
* title: Word Pattern
* content:
* 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.
*
*/
public class WordPattern {
//这个地方犯了两次错误,怎么就一直不改正呢
//没有把key不同,value相同的情况排除掉 即没有排除:abab,dog dog dog dog这种情况
public boolean wordPattern(String pattern, String str){
if(pattern == null || str == null) return false;
int lengthP = pattern.length();
String[] strArray = str.split(" ");
int lengthS = strArray.length;
if(lengthP == 0 || lengthS == 0 || lengthP != lengthS) return false;
HashMap<Character,String> container = new HashMap<Character,String>();
for(int i=0;i<lengthP;i++){
if(container.containsKey(pattern.charAt(i))){
if(!container.get(pattern.charAt(i)).equals(strArray[i])){
return false;
}
}else{
if(container.containsValue(strArray[i])){
return false;
}else{
container.put(pattern.charAt(i), strArray[i]);
}
}
}
return true;
}
//大牛的算法,这里学习了index.put(key,value)的返回值,根据key来判断
/*
* 这是最原始的put的返回值的注释
* the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>,
* if the implementation supports <tt>null</tt> values.)
* 大意是如果put的key值原来并不在map中,则返回null,如果key已经存在了,那么返回key值对应的前一个value值
* 用这种方法判断,key存放的pattern的每一个字符,与str的每一个单词,然后根据对应的位置value来判断是否是一一影射
* 这里注意一下返回值是一个对象
* */
public boolean wordPatterns(String pattern, String str){
String[] words = str.split(" ");
if(words.length != pattern.length())
return false;
Map index = new HashMap();
for(Integer i=0;i<words.length;i++){//这里一开始用int报错了
if(index.put(pattern.charAt(i),i) != index.put(words[i],i))
return false;
}
return true;
}
public static void main(String[] args) {
WordPattern word = new WordPattern();
word.wordPatterns("abba", "dog dog cat cat");
}
}
Word Pattern的更多相关文章
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- [LeetCode] Word Pattern
Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...
- Word Pattern | & II
Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...
- 291. Word Pattern II
题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- Word Pattern II 解答
Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- 【leetcode】290. Word Pattern
problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
- 290. Word Pattern 单词匹配模式
[抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
随机推荐
- 清理yum源
最近想在美国VPS上装个Wine 可是在执行yum install wine时却遇到了意想不到的错误 系统提示: You could try using –skip-broken to work ar ...
- windows10UWP:如何判断一个文件或者文件夹是否存在?
使用 StorageFolder.TryGetItemAsync 方法,尝试按名称获取文件或文件夹,不需将错误捕捉逻辑添加到代码(就像使用 StorageFolder.GetItemAsync 一样) ...
- codevs 2822爱在心中
不想吐槽题目.... /* K bulabula 算法(好像用哪个T bulabula更简单 然而我并不会 - -) 丑陋的处理cnt: Printf时 cnt中 ans[i][0]==1 的删掉 然 ...
- html获取gps坐标
<script> function getLocation(){ var options={ enableHighAccuracy:true, maximumAge:1000 } if(n ...
- 正则表达式匹配(node.js)
参考文档如下: http://blog.csdn.net/huiguixian/article/details/6131048 http://www.91xueke.com/2013/04/05/30 ...
- TFS 服务器更换后工作区无法绑定
需要删除工作区,删除命令如下 tf workspace /delete 工作区名;创建的用户 /server:TFS服务器 例 tf workspace /delete WHQ-PC;whq /ser ...
- android EventBus 的使用
今天简单的介绍 一下啊 android EventBus 的使用 EventBus 在官方介绍中是订阅......什么的 一大堆 , 在我android 菜鸟眼里 就是用来代替android 广 ...
- Const和ReadOnly
总结一下const和readonly有这么几条区别: const和readonly的值一旦初始化则都不再可以改写: const只能在声明时初始化:readonly既可以在声明时初始化也可以在构造器中初 ...
- 自己动手写一个iOS 网络请求库的三部曲[转]
代码示例:https://github.com/johnlui/Swift-On-iOS/blob/master/BuildYourHTTPRequestLibrary 开源项目:Pitaya,适合大 ...
- UIDatePikcer的基本用法
- (void)viewDidLoad { [super viewDidLoad]; _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectM ...