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. 获取地址上的ID(页面与服务器的交互)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. js数字时钟

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. SQL_函数

    五毛叶 — SQL_函数: 如下: 1 SQL_Aggregate函数 AVG() - 返回平均值 COUNT() - 返回行数 FIRST() - 返回第一个记录的值 LAST() - 返回最后一个 ...

  4. java中文乱码分析整理

    在JavaWeb应用开发中,经常会出现页面中本该显示中文的地方却是乱码的情况.究其原因,主要是由于在Web组件之间.或Web组件与浏览器.与数据库所使用的字符集标准不统一,Web应用程序运行过程中,中 ...

  5. FPGA书籍

    Xilinx FPGA开发实用教程(第2版) 徐文波,田耘 著

  6. 另类分析SIGSEGV信号

    关于SIGSEGV信号的含义就不解释了.网络上有很多解释. 今天记录一下,自己遇到的一个问题,想了好几天都没想出来的.今天终于想到原因了. 过程描述: 有个类 CBase,里面放了一个成员变量 DAT ...

  7. LeetCode-Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  8. [课程设计]Scrum 1.6 多鱼点餐系统开发进度

    [课程设计]Scrum 1.6 多鱼点餐系统开发进度(点餐页面按钮添加&修复) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4. ...

  9. ado.net五大对象

    五大对象分别是: 1. Connection:与数据源建立连接. 2. Command:对数据源执行SQL命令并返回结果. 3. DataReader:读取数据源的数据,只允许对将数据源以只读.顺向的 ...

  10. 【Unity3D基础教程】给初学者看的Unity教程(五):详解Unity3D中的协程(Coroutine)

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 为什么需要协程 在游戏中有许多过程(Proc ...