LeetCode 290. Word Pattern (词语模式)

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.



题目标签:Hash Table

  题目给了我们一个pattern 和一个 str, 让我们判断,str 是否和 pattern 一致。

  首先,把str split 到 words array 里,如果pattern 的长度 和 words 长度 不一样,直接返回false;

  接下来利用HashMap,把pattern 里的 char 当作key, 每一个word 当作value 存入,如果遇到一样的char,但是有着不一样的value,返回false;如果遇到不一样的char,但有着一样的value,返回false;最后遍历完之后,返回true。

Java Solution:

Runtime beats 36.87%

完成日期:06/05/2017

关键词:HashMap

关键点:char 当作 key,word 当作 value

 1 class Solution
 2 {
 3     public boolean wordPattern(String pattern, String str)
 4     {
 5         HashMap<Character, String> map = new HashMap<>();
 6
 7         String[] words = str.split(" ");
 8
 9         if(pattern.length() != words.length)
10             return false;
11
12         for(int i=0; i<pattern.length(); i++)
13         {
14             char c = pattern.charAt(i);
15
16             if(map.containsKey(c))
17             {
18                 if(!map.get(c).equals(words[i]))
19                     return false;
20             }
21             else
22             {
23                 if(map.containsValue(words[i]))
24                     return false;
25
26                 map.put(c, words[i]);
27             }
28         }
29
30         return true;
31     }
32 }

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

时间: 2024-07-31 14:31:32

LeetCode 290. Word Pattern (词语模式)的相关文章

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

翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pattern = "abba". str = "dog cat cat dog" 应该返回真. pattern = "abba", str = "dog cat cat fish" 应该返回假. pattern = "aaa

LeetCode 290. Word Pattern

不符合的情况有三种:1.pattern和str长度不符   2.pattern中不同字母指向str中同一词(a.b->同一词)   3.同一字母指向不同词(a->不同词) 1 class Solution { 2 public: 3 vector<string> split(string str){ 4 vector<string> res; 5 string tmp = ""; 6 for(int i = 0; i < str.length(

LeetCode:Word Pattern - 字符串模式匹配

1.题目名称 Word Pattern(字符串模式匹配) 2.题目地址 https://leetcode.com/problems/word-pattern/ 3.题目内容 英文:Given a pattern and a string str, find if str follows the same pattern. 中文:给出一组模式(pattern)和一个字符串(str),查看字符串是否与模式匹配 例如: pattern = "abba",str = "dog cat

[LeetCode] 291. Word Pattern II 词语模式 II

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 substring in str. Examples: pattern = "abab", str = "redbluer

290. Word Pattern

/* * 290. Word Pattern * 2016-7-2 by Mingyang * 这里加上了没有containsValue,因为这里如果abba 和 dog dog dog dog通不过, * 因为a已经包含dog了,b却也包含了dog,解决方法就是value不能重复 * 直接用containsValue就好了 */ public static boolean wordPattern(String pattern, String str) { String[] array=str.

[LeetCode][JavaScript]Word Pattern

Word Pattern 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 substring in str. Examples: pattern = "abba", str = "dog c

【一天一道LeetCode】#290. Word Pattern

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 pa

290. Word Pattern(LeetCode)

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 d

啊啊啊,真的没啥东西好写啊【leetcode】290. Word Pattern

public class Solution { public boolean wordPattern(String pattern, String str) { String[] s=str.split(" "); if(pattern.length()!=s.length) return false; for(int i=0;i<pattern.length();i++) { for(int j=0;j<i;j++) { if((pattern.charAt(i)!=pa