Easy-题目49:290. Word Pattern

原创 2016年05月30日 20:42:55

题目原文:
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:
pattern = “abba”, str = “dog cat cat dog” should return true.
pattern = “abba”, str = “dog cat cat fish” should return false.
pattern = “aaaa”, str = “dog cat cat dog” should return false.
pattern = “abba”, str = “dog dog dog dog” should return false.
题目大意:
给出一个模式和字符串。判断字符串时候满足模式。这里模式和对应字符串要求是一一映射(bijection)的。(如果不知道啥叫一一映射,继续滚回去复习高中数学!)
题目分析:
这题和Easy第46题类似,使用两个HashMap维护双向的key-value对,一旦发现有重复的key返回false。
源码:(language:java)

public class Solution {
    public boolean wordPattern(String pattern, String str) {
        HashMap<Character,String> map=new HashMap();
        HashMap<String,Character> map2=new HashMap();
        String[] words=str.split(" ");
        if(pattern.length()!=words.length)
            return false;
        for(int i=0;i<pattern.length();i++) {
            Character key=pattern.charAt(i);
            String word=words[i];
            if(!map.containsKey(key))   {
                if(!map2.containsKey(word))
                {
                    map.put(key, word);
                    map2.put(word, key);
                }
                else
                    return false;
            }               
            else if (!(map.get(key).equals(word) && map2.get(word).equals(key)))
                return false;
        }
        return true;
    }

}

成绩:
3ms,beats 18.29%,众数3ms,51.82%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

290. Word Pattern [easy] (Python)

题目链接https://leetcode.com/problems/word-pattern/题目原文 Given a pattern and a string str, find if str ...

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
  • 377

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Easy-题目49:290. Word Pattern
举报原因:
原因补充:

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