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. OpenGL管线(用经典管线代说着色器内部)

    图形管线(graphics pipeline)向来以复杂为特点,这归结为图形任务的复杂性和挑战性.OpenGL作为图形硬件标准,是最通用的图形管线版本.本文用自顶向下的思路来简单总结OpenGL图形管 ...

  2. js实现图片实时预览

    注: 此博客转自 http://www.cnblogs.com/goody9807/p/6064582.html  转载请注明出处 <body> 上传图片: <input type= ...

  3. Github注册账户过程

    首先打开网站https://github.com/ 第一个是输入用户名第二个是邮箱第三个是自己的密码然后点    sign up for github 此时会有个邮箱注册的验证进入注册的邮箱点击认证. ...

  4. js中toString和valueOf方法的区别

    toString 方法 返回对象的字符串表示形式. 语法:objectname.toString([radix]) objectname 必需.要为其搜索字符串表示形式的对象. radix 可选.为将 ...

  5. 关于django xadmin的学习改造(菜单名称,更改默认前缀数据库)

    路径xadmin-master\demo_app\app\models.py class c(models.Model): ip_address = models.CharField(max_leng ...

  6. Express4.x常用API(一):res

    最近在学习NodeJS,用到了express,看着官网上的API手册,打算把其中比较常用到的API根据自己理解翻译一下,方便自己学习使用. 该篇打算用来记录下express中res. 由于水平有限,希 ...

  7. 【引】objective-c,3:关于block

    原文参考博文: http://blog.devtang.com/2013/07/28/a-look-inside-blocks/ http://www.cnblogs.com/kesalin/arch ...

  8. 关于 一开始不懂得 hosts配置。

    是转载别人的. 原文: http://my.oschina.net/cxz001/blog/298228   感谢分享: 一开始抄着陪着win下的hosts文件.然后配置  apache中的hosts ...

  9. 安卓开发学习经历2--《第一行代码》coolweather项目SQL语句同一个“陷阱”掉两次 注意转义字符等特殊字符正确书写 关于Id字段自增加体会

    今天,在运行<第一行代码>coolweather第二阶段代码,又一次报错,还是神奇地与昨天相似,提示,city_id字段不存在,这里我有两种理解,一种是sql语句出错了,另外一种是没有获取 ...

  10. 跟我学Windows Azure 四 Cloud Service中的WebRole与WorkRole,及他们之间的通信

    Cloud Service 中WebRole就相当与我们的WebSite,而WorkRole相当与我们在服务器上写了个Windows Service,站在高可用的角度上来讲,Cloud Service ...