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 ...
随机推荐
- Apache SSL服务器配置SSL详解(转)
1.安装必要的软件 引用 我用的是apahce2.0.61版,可以直接官方提供的绑定openssl的apache. 文件名是:apache_2.0.61-win32-x86-openssl-0.9.7 ...
- android 17 activity生命周期
手机指南针传感器处于手机头部. Activity生命周期: 启动. onCreat()方法:初始化布局对象,设置监听器. onstart()方法:注册监听器. onResume():activity已 ...
- Map的迭代操作
Map的迭代操作 public static void main(String[] args) { Map<String, List<Integer>> map = new H ...
- 快速记忆JavaScript中exec和match的使用
JS模式匹配中exec,match用得非常多,所以掌握其用法对我们进行字符串的处理帮助非常大 1.exec的定义其用法 exec与match主要的不同是,exec是正则表达式里面的方法. ...
- android listview 替代品recyclerview详解
安卓v7支持包下的ListView替代品————RecyclerView RecyclerView这个控件也出来很久了,相信大家也学习的差不多了,如果还没学习的,或许我可以带领大家体验一把这个艺术 ...
- 使用Sqlite数据库存储数据
1.Sql基本命令 1.1.创建表 表是有行和列组成的,列称为字段,行称为记录. 使用CREATE命令来创建表: 1 CREATE TABLE tab_student (studentId INTEG ...
- (ternary operator)三元运算符.
ternary operator: advantage: make a terse simple conditional assignment statement of if-then-else. d ...
- Android平台的四大天王:Activity, Service, ContentProvider, BroadcastReceiver
今天开始要自学android,刚看到百度知道上面这段话,觉得不错(不过已经是2011年8月的回答了): Android系统的手机的每一个你能看到的画面都是一个activity,它像是一个画布,随你在上 ...
- PAT_2-08. 用扑克牌计算24点
一副扑克牌的每张牌表示一个数(J.Q.K分别表示11.12.13,两个司令都表示6).任取4张牌,即得到4个1~13的数,请添加运算符 (规定为加+ 减- 乘* 除/ 四种)使之成为一个运算式.每个数 ...
- Nagios新添加的hosts和services有时显示,有时不显示问题解决
在nagios配置文件hosts.cfg和services.cfg中添加了新服务器和服务列表,重启nagios服务后刷新监控页面,新添加的服务器和服务列表有时能显示出来,有时又显示不出来. 解决方法: ...