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 ...
随机推荐
- Javascript 函数和模块定义
匿名函数 // calculator.js(function(root) { var calculator = { sum: function(a, b) { return a + b; } ...
- 一个很简单的SqlServer生成常用C#语句工具的诞生
前言: 这个文章只要是记录一下,这个工具的诞生过程.作用.其中的技术实在是太简单可以说没有什么技术~主要是锻炼一下写文章的能力! 正文: 在开发项目的时,常常会要维护或变更一些老项目,涉及到简单的几张 ...
- Android性能优化典范 - 第5季
这是Android性能优化典范第5季的课程学习笔记,拖拖拉拉很久,记录分享给大家,请多多包涵担待指正!文章共10个段落,涉及的内容有:多线程并发的性能问题,介绍了AsyncTask,HandlerTh ...
- Java设计模式02:常用设计模式之工厂模式(创建型模式)
一.工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的. 工厂模式在<Java与模式>中分为三类: 1)简单工厂模式(Simple Fact ...
- (转)javaScript call 函数的用法说明
call 方法 请参阅 应用于:Function 对象 要求 版本 5.5 调用一个对象的一个方法,以另一个对象替换当前对象. call([thisObj[,arg1[, arg2[, [,.argN ...
- JS 模拟C# 字符串格式化操作
/*** ** 功能: 字符串格式化替换操作 ***/ String.prototype.format = function () { var args = arguments; return thi ...
- php验证是否为手机端还是PC
<?php $forasp = strtolower($_SERVER['HTTP_USER_AGENT']); if(strpos($forasp,'mobile')==true) { ech ...
- 后台线程,优先级,sleep,yield
1.后台线程,是指在程序运行的时候在后台提供一种通用服务的线程,并且这种线程并不属于程序中不可获取的部分.当所有非后台线程结束时,程序也就 终止了,同时会杀死进程中所有后台线程.main()是一个非后 ...
- Asp.net MVC利用Ajax.BeginForm实现bootstrap模态框弹出,并进行前段验证
1.新建Controller public ActionResult Index() { return View(); } public ActionResult Person(int? id) { ...
- weChat聊天发送图片带有小尖角的实现
weChat聊天发送图片带有小尖角的实现 1.#import <UIKit/UIKit.h>2.3.@interface JKShapeImage : UIView4.5.@propert ...