290. Word Pattern*

原创 2017年01月03日 21:41:29

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.

Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.

    public boolean wordPattern(String pattern, String str) {
        String[] words = str.split(" ");
        if(words.length!=pattern.length()) return false;
        Map index = new HashMap();
        for(Integer i=0; i<words.length;i++){
            if(index.put(pattern.charAt(i), i)!=index.put(words[i], i)) return false;
        }
        return true;
    }

总结:put 返回之前的值,如果map中存在某个key,且该key之前的值pattern match, 那么说明目前判断的也match
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode290. Word Pattern & 205. Isomorphic Strings

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

leetcode 290: Word Pattern

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

LeetCode(290) Word Pattern

题目Given a pattern and a string str, find if str follows the same pattern.Here follow means a full ma...
  • fly_yr
  • fly_yr
  • 2015年11月23日 13:33
  • 380

290 word pattern

一. 题目来源: lettcode: 290 word pattern 二, 题目大意: 给定两个字符串,其中一个是模板,一个是匹配串,模板是如aabb型的字符串,匹配串是由空格隔开的几个字符串 比如...

Leetocde_290_Word Pattern

http://blog.csdn.net/pistolove/article/details/49717803 Given a pattern and a string str, fi...

290-e-Word Pattern

判断字符与字符串是否成对对应,例如pattern = "abba", str = "dog cat cat dog" 返回true,如果str = “dog cat cat cat”则返回false,...

LeetCode笔记:290. Word Pattern

判断字符串中的单词是否匹配模型

290. Word Pattern(技巧:记录当前位置来判断两个字符串是否符合)

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

leetcode题解-205.Isomorphic Strings && 290. Word Pattern

题目:Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the c...

290. Word Pattern

Given a pattern and a string str, find if str follows the same pattern. Here follow means a ful...
  • x_shuck
  • x_shuck
  • 2016年03月25日 17:20
  • 73
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:290. Word Pattern*
举报原因:
原因补充:

(最多只允许输入30个字)