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:

  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.

思路:使用两个map记录是否pattern和str匹配。

注意,map的键值这里不能为0,因为为0时map默认是访问了未创建过的键,因此要从1开始。

此外,for循环里真的可以实现很多东西,用好了会让代码很精简。

 1 class Solution {
 2 public:
 3     bool wordPattern(string pattern, string str) {
 4         map<char, int> dict1;
 5         map<string, int> dict2;
 6         stringstream ss(str);
 7         int i = 0, n = pattern.size();
 8         for (string word; ss >> word; i++)
 9         {
10             if (i == n || dict1[pattern[i]] != dict2[word])
11                 return false;
12             dict1[pattern[i]] = dict2[word] = i + 1;
13         }
14         return i == n;
15     }
16 };
时间: 2024-11-01 14:45:40

Word Pattern - LeetCode的相关文章

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 Word Pattern II

原题链接在这里:https://leetcode.com/problems/word-pattern-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

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][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] 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

LeetCode_290. Word Pattern

290. Word Pattern Easy 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. Example 1: Input: pattern = "abba&q

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.

Word Pattern

?package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** *  * @author zhiyong wang * title: Word Pattern * content: * Given a pattern and a string str, find if str follows the same pattern. * Here follow means a ful

Word Pattern II 解答

Question 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 = "