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. 在 Windows 下部署 Vagrant

    Vagrant 是一个可创建轻便.可复用的虚拟开发环境的跨平台工具,通过打包分可使所有开发成员使用统一的开发环境. 下面是我自己记录配置 Vagrant 的过程. 1.安装 VirtualBox 略. ...

  2. linux find

    find 命令用于查找文件系统中的指定文件,其命令格式为:find   要查找的路径   表达式例如:find . -name 1.txt      在当前目录及其子目录下查找文件 1.txtfind ...

  3. PHP 输入流 php://input

    在使用xml-rpc的时候,server端获取client数据,主要是通过php输入流input,而不是$_POST数组.所以,这里主要探讨php输入流php://input   对一php://in ...

  4. tcp之快速重传与恢复

    本文为原创,转载请注明:http://www.cnblogs.com/gistao/ Background 写网络程序的都知道,tcp的窗口控制分为慢启动阶段和拥塞避免阶段,重传机制有快速重传/恢复和 ...

  5. 解决Oracle在scott用户下创建视图(VIEW)权限不足的方法

    问题描述:在scott用户下创建视图的时候,报错:权限不足.(其他用户以此类推)解决方法: 以dba用户登录 sqlplus / as sysdba 赋予scott用户创建VIEW的权限 grant  ...

  6. 在项目中导入MRC的文件时解决办法

    1.由于在项目中要使用到第三方框架和其他的类的时候,而它用的是MRC的时候,其最简便的方法:完成从MRC到ARC的转换. 1.点击工程文件,进入到工程的设置里面. 2.看见Build Phases,就 ...

  7. April Fools Day Contest 2014

    April Fools Day Contest 2014 A.C.H三道题目 ============================================================= ...

  8. Java中抽象类和接口

    抽象类: 为什么要用抽象类? 1.父类的方法好多情况下是没有内容的.例如:USB是一个父类,里面的方法的函数体是可以不写,通过子类可以重写. 2.万一子类没有重写正确,是没有没有提示的.例如:父类中函 ...

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

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

  10. 从DataTable获取Json数据

    public string GetJson(DataTable dt){ JavaScriptSerializer jss=new JavaScriptSerializer(); jss.MaxJso ...