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. C++中的异常处理机制

    C++中的捕获异常机制catch参数中实参的类型不同,采取的处理方式则不相同,且与普通的函数调用还不一样,具体表现为当抛出异常throw A()或throw obj时,对象会进行一次额外的对象复制操作 ...

  2. AIX上通过IPSEC进行IP包过滤

    AIX上的IPSEC 在AIX可以通过以下步骤打开IP Security smitty ipsec4 --> Start/Stop IP Security --> Start IP Sec ...

  3. Ubuntu W: GPG error: http://archive.ubuntukey....NO_PUBKEY 8D5A09

    在用 sudo apt-get update 时出现这样的报错: W: GPG error: http://archive.ubuntukylin.com:10006/ubuntukylin xeni ...

  4. 页面加载完后自动执行一个方法的js代码

    1.在body中用onload: <body onload="conver()"> 2.在脚本中用window.onload: <script type=&quo ...

  5. JS 设计模式

    1.单例模式:产生一个类的唯一实例 例如:我们在页面中添加遮罩层,每次只能有一个遮罩层存在,因此为单例模式. 在创建遮罩层之前判断是否已经存在,若没有存在,则创建. 这里使用闭包,将是mask变量封装 ...

  6. Adobe Flash builder 4的序列号

    下载flex 4 之后,需要输入注册码(如果购买了正版就要选择第二个的单选框,无需输入注册码了) 1424-4258-9368-0713-8534-5128 1424-4806-8312-7960-9 ...

  7. 初识Linux—1

    1,Ctrl+C作用是终止当前的命令 2,ps显示目前正在执行的程序(命令)(process status) 3,退出是exit,连续按exit,最终会关闭终端 4,Root是管理员,其他的用户都是由 ...

  8. Oracle、MySql、SQLServer数据分页查询

    看过此博文后Oracle.MySql.SQLServer 数据分页查询,在根据公司的RegionRes表格做出了 SQLserver的分页查询语句: 别名.字段 FROM( SELECT row_nu ...

  9. VBA编程常用语句

    .Option Explicit '强制对模块内所有变量进行声明 Option Private Module '标记模块为私有,仅对同一工程中其它模块有用,在宏对话框中不显示 Option Compa ...

  10. 自建yum源及分组安装

    最近在研究一套自动化运维方向的框架,在想到远程安装软件包的时候,觉得有yum支持会更方便一些.主要思路是把程序员写的代码或程序打包成rpm,然后提交到自建yum源,并实现按组安装,本地yum源实现步骤 ...