​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. ReentrantLock与synchronized的差别

    总的来说,lock更加灵活. 主要同样点:Lock能完毕synchronized所实现的全部功能 不同: 1.ReentrantLock功能性方面更全面,比方时间锁等候,可中断锁等候,锁投票等,因此更 ...

  2. 《Linux内核分析》-----张超

    http://www.cnblogs.com/zhangchao0515/category/797954.html MOOC课程 http://mooc.study.163.com/course/US ...

  3. 还在用ListView?

    还在用Lisview?RecyclerView都已经出来一年多了! 想必大家多或多或少的接触过或者了解过RecyclerView,为什么没有用起来,原因大概如下? ListView我用的挺好的,为什么 ...

  4. spring 定时任务的 执行时间设置规则

    单纯针对时间的设置规则org.springframework.scheduling.quartz.CronTriggerBean允许你更精确地控制任务的运行时间,只需要设置其cronExpressio ...

  5. http学习笔记一

  6. 关于WINDOWS命令

    1. Windows netstat 查看端口.进程占用 netstat -aon —  显示全部进程 2. 查看进程命令 tasklist — 显示全部进程 taskkill — 关闭至少一个系统进 ...

  7. 使用Spring简化JDBC操作数据库

    Spring的开发初衷是为了减轻企业级开发的复杂度,其对数据库访问的支持亦如此,使用Spring访问数据库能带来以下好处: 1.1     简化代码 使用原生的JDBC访问数据库,一般总是要执行以下步 ...

  8. ASP.NET Excel数据导出数据库

    /// <summary> /// 根據gridview導出excel /// </summary> /// <param name="ctl"> ...

  9. Eclipse中点击小猫提示Tomcat settings should be set in Tomcat Preference Page

    1.window->preference->tomcat->tomcat-version选择自己tomcat版本 tomcat home 选择tomcat安装目录,即bin的上一层 ...

  10. 修改docker默认172.17网段

    docker启动时默认使用172.17.x.x作为容器的ip地址,可以通过以下方法自定义该网段: sudo service docker stop通过命令route -n查看docker0是否存在,若 ...