[leetcode]Word Pattern

原创 2015年11月19日 21:38:59

题目如下:

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.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.

看到这个题目下意识地就选择用map,考虑以下几个问题:

  1. 初始输入的两个字符串,用split方法将后一个进行分割并保存在一个string类型的数组中,注意比较一下数组的长度与第一个字符串的长度是否相一致,不一致直接返回false(特殊情况的考虑);
  2. 在使用map时不单单要对key进行判断,还需要对value进行判断,否则会有特殊案例无法通过(因为这个WA了一遍)

毕竟是Easy题,附上代码:

public class Solution {
    public boolean wordPattern(String pattern, String str) {
        boolean flag = true;
            Map<Object, String> map = new HashMap();
            String []strArray = str.split(" ");
//          System.out.println(strArray.length);
            if(pattern.length() != strArray.length) return false;
            int index;
            char ch;
            for(index= 0; index < pattern.length(); index++){
                ch = pattern.charAt(index);
                if(map.containsKey(ch)){
                    String key = map.get(ch);
                    if(!key.equals(strArray[index])){
                        flag = false;
                        break;
                    }
                }
                else{
                    if(map.containsValue(strArray[index])){
                        flag = false;
                        break;
                    }
                    map.put(ch, strArray[index]);
                }
            }           
            return flag;
    }
}

题目链接:https://leetcode.com/problems/word-pattern/

版权声明:本文为博主原创文章,未经博主允许不得转载。

leetcode 哈希表专题-Word Pattern

记录下leetcode的题目:word pattern

LeetCode笔记:290. Word Pattern

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

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...

<LeetCode OJ> 290. Word Pattern

290. Word Pattern My Submissions Question Total Accepted: 24989 Total Submissions: 89440 Difficu...

LeetCode 290 word pattern 的一点疑惑

LeetCode第290题,题目大致意思是给定两个字符串,判断字符串2的模式是否与字符串1相同,也就是说对应位置的字符是否相同。如: Examples: 1.pattern = "abba", s...

LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

翻译给定一个模式,和一个字符串str,返回str是否符合相同的模式。这里的符合意味着完全的匹配,所以这是一个一对多的映射,在pattern中是一个字母,在str中是一个为空的单词。例如: patter...
  • NoMasp
  • NoMasp
  • 2016年01月30日 15:43
  • 1739

leetcode题目:Word Pattern (Javascript版)

leetcode题目:Word Pattern (Javascript版)

【Leetcode】word pattern

Since I have an interview tomorrow, I decide to brush some easy level question of Leetcode and it tu...

LeetCode 2 Word Pattern

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

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]Word Pattern
举报原因:
原因补充:

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