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:

  1. pattern = "abba", str = "dog cat cat dog" should return true.
  2. pattern = "abba", str = "dog cat cat fish" should return false.
  3. pattern = "aaaa", str = "dog cat cat dog" should return false.
  4. pattern = "abba", str = "dog dog dog dog" should return false
    public class Solution {       //if不用hashmap,更好的方法是设置头尾“指针”,保证一个指向当前的值
    public boolean wordPattern(String pattern, String str) {
    String[] strs = str.split(" ");
    if (pattern.length() != strs.length)
    return false;
    Map<Character, String> map = new HashMap<Character, String>();
    for (int i = 0; i < pattern.length(); i++) {
    if (map.containsKey(pattern.charAt(i))
    && !(map.get(pattern.charAt(i))).equals(strs[i]))
    return false;
    if (!map.containsKey(pattern.charAt(i))
    && map.containsValue(strs[i]))
    return false;
    if (!map.containsKey(pattern.charAt(i))
    && !map.containsValue(strs[i]))
    map.put(pattern.charAt(i), strs[i]);
    }
    return true;
    }
    }

      

(String). 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. 290. Word Pattern

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

  7. leetcode面试准备: Word Pattern

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

  8. Word Pattern

    ​package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** *  * @au ...

  9. Word Pattern II 解答

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

随机推荐

  1. Atom 如何隐藏 .Ds_Store 文件

    明明在core里设置了忽略文件列表,但是在右侧的文件列表中还是看到了,相当烦人.然后我查了下资料,原来还需要核心插件TreeView再设置一次遵循全局忽略文件才生效. 版本:1.12.6  (其它版本 ...

  2. dede currentstyle属性完美解决方案

    问题一.dede让channelartlist标签支持currentstyle属性 完美解决 打开include\taglib\channelartlist.lib.php找到$pv->Fiel ...

  3. java基础小知识

    1.1常量: 基本数据类型常量 字符常量 整数常量的表现形式:一进制的形式来表示(二进制,八进制,十进制,十六进制) 生活中:十进制(0-9)  ,星期(七进制(0-6)) ,时间(十二进制(0-11 ...

  4. div隐藏与显示

    <input type="button" value="隐藏详情" class="jishu_n_k1_input2" id=&quo ...

  5. music player界面

    public class SoundPlayerGUI extends JFrame implements ChangeListener, ActionListener { private stati ...

  6. CE STEPLDR

    作用:初始化CPU.内存.Flash,复制EBoot到内存并跳入EBoot中运行. 原理:S3C2416有 8-KB 的steppingstone(暂时翻译为垫脚石),在Nand启动模式下可把Nand ...

  7. dependency of static library

    一直以来都有一个误区,认为静态库就一定是不含任何依赖的,动态库是含的.这个印象是因为在我们程序中,包含静态库的地方,往往Build好之后直接就可以用,而含DLL的地方,则需要在build好之后的EXE ...

  8. Linux测试环境部署tomcat(二)

    安装配置tomcat 1:在http://tomcat.apache.org/download-80.cgi下载最新安装包 2:安装tomcat 将apache-tomcat-8.0.21.tar.g ...

  9. 自动化测试工具QTP的使用实例 分类: 软件测试 2015-06-17 00:23 185人阅读 评论(0) 收藏

    1. QTP简介 1.1QTP功能与特点 QTP是QuickTest Professional的简称,是一种自动化软件测试工具.在软件的测试过程中,QTP主要来用来通过已有的测试脚本执行重复的手动测试 ...

  10. java获得某个月的天数

    /*** 方法一*/ String strDate = "2012-02"; SimpleDateFormat format = new SimpleDateFormat(&quo ...