​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的更多相关文章

  1. [LeetCode] Word Pattern II 词语模式之二

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  2. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  3. [LeetCode] Word Pattern

    Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...

  4. Word Pattern | & II

    Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...

  5. 291. Word Pattern II

    题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...

  6. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

  7. Word Pattern II 解答

    Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  8. 【leetcode】290. Word Pattern

    problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...

  9. 290. Word Pattern 单词匹配模式

    [抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

随机推荐

  1. Active Desktop--桌面字体背景被修改

    怎么修改回来 步骤如下 方法一.在桌面上点击右键 -- 排列图标 -- 去掉“在桌面上锁定Web项目”上的勾. 方法二.右键点击我的电脑 -- 属性 -- 高级 -- 点击“性能”下面的“设置”按钮, ...

  2. Linux试玩指令开机关机

    Linux内核最初只是由芬兰人李纳斯·托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的. Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和U ...

  3. TabHost理解与使用

    一.继承关系 java.lang.Object ↳ android.view.View ↳ android.view.ViewGroup ↳ android.widget.FrameLayout ↳ ...

  4. 点击文字可以选中相应的checkbox

    <html><head><title>中国站长天空-网页特效-表单特效-点击文字选中的复选框</title><meta http-equiv=&q ...

  5. Array,ArrayList 和 List<T>的选择和性能比较.

    Array Class Provides methods for creating, manipulating, searching, and sorting arrays, thereby serv ...

  6. (转)asp.net分页存储过程

    Asp.Net分页存储过程 SQL分页语句 一.比较万能的分页: sql代码: 1 2 3 select top 每页显示的记录数 * from topic where id not in  (sel ...

  7. oracle 数据库关闭的的几种方式总结

    shutdown的几种方式,shutdown abort的一些弊端有哪些   1.shutdown normal        正常方式关闭数据库.    2.shutdown immediate   ...

  8. Python:函数定义

    #!/usr/bin/python3 #函数 def add(a,b): return a+b print("add(2,5) = ",add(2,5)) def add2(a,b ...

  9. Syntax error on token "package", assert expected------踩坑记录

    今天写程序碰到个坑,eclipse编辑,jdk1.7,clean编译项目后报错Syntax error on token "package", assert expected 反复 ...

  10. functools学习有感

    functools的内容不多,包含四个函数(partial,reduce,update_wrapper,wraps)和一个python对象(partial Objects). functools的四个 ...