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 ...
随机推荐
- android 21 隐式意图启动系统预定义activity
Intent intent=new Intent(LoginActivity.this, MainActivity.class);//显示意图启动,显示从一个activity到另一个activity, ...
- ios-点击屏幕,隐藏键盘
ios-点击屏幕,隐藏键盘 - (void)getFirstRegist{ //结束键盘编辑 __weak typeof(self)weakSelf = self; UITapGestureRecog ...
- (转载)JavaScript中面向对象那点事
鉴于自己在JavaScript这方面比较薄弱,所以就找了一本书恶补了一下(被称为犀利书的JavaScript权威指南).书的内容虽然多了点,但这也充分说明了js中的东西还是挺多的.虽然我们的定位不是前 ...
- 为什么你需要使用instancetype而不是id
四年前Clang添加了关键字instancetype,目的在于取代-alloc和-init等方法的返回类型id,那么使用instancetype到底比id好在哪里? instancetype宣言 不管 ...
- 解决Chrome谷歌浏览器不支持CSS设置小于12px的文字
在最新版的谷歌里.已经不在支持这个属性啦 谷歌浏览器Chrome是Webkit的内核,有一个 -webkit-text-size-adjust 的私有 CSS 属性,通过它即 可实现字体大小不随终端设 ...
- Android中Cursor类的概念和用法
http://blog.sina.com.cn/s/blog_618199e60101fskp.html 使用过 SQLite数据库的童鞋对 Cursor 应该不陌生,加深自己和大家对Android ...
- 美好头标ToolBar
ActionBar我相信是每一位合格的程序员都用过的组件,也是每一个程序员都会抱怨的组件,因为他不能实现复杂的自定义.为此Google推出了比ActionBar更为美好的组件ToolBar. 本文重点 ...
- Express在windows IIS上部署详解
最近公司在用Express+angularjs+wcf开发系统,让我在windows上部署系统,遇到不少问题,不过最后还是解决了,在IIS上部署系统, 首先windows需安装以下软件: 1.node ...
- 收集 数据库的awr数据,生成报告
该脚本只是把awr报告的内容,原封不动的 展现出来,做记录 awrreport.sql 脚本内容如下: *********************************************** ...
- 打造属于前端的Uri解析器
今天和大家一起讨论一下如何打造一个属于前端的url参数解析器.如果你是一个Web开发工程师,如果你了解过后端开发语言,譬如:PHP,Java等,那么你对下面的代码应该不会陌生: $kw = $_GET ...