【LeetCode】290. Word Pattern 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/word-pattern/#/description
题目描述
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.
题目大意
看模式和空格分割的字符串模式能不能构成一一映射。
解题方法
这个题第一感觉就是使用HashMap,看到Tags也是HashMap我就放心了。想法基本就是把字符和单词对应起来,看单词和字符能不能对应上,这样就知道模式是否匹配了。做的时候出现了一个小问题,就是pattern = "abba", str = "dog dog dog dog"这种,要判断是不是已经放进了value,之前不知道怎么办,今天学到了HashMap有个containsValue()方法,可以判断是否已经放进了value。
public class Solution {
public boolean wordPattern(String pattern, String str) {
HashMap<Character, String> map = new HashMap<Character, String>();
String[] words = str.split(" ");
if(words.length != pattern.length()){
return false;
}
for(int i = 0; i < words.length; i++){
String word = words[i];
char temp = pattern.charAt(i);
if(map.containsKey(temp)){
if(!map.get(temp).equals(word)){
return false;
}
}else{
if(map.containsValue(word)){
return false;
}
map.put(temp, word);
}
}
return true;
}
}
二刷,使用Python解法,这个题和205. Isomorphic Strings一模一样,所以很快就写出来这个一一映射的代码:
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
strs = str.split()
if len(pattern) != len(strs):
return False
d = dict()
for i, p in enumerate(pattern):
if p not in d:
d[p] = strs[i]
else:
if d[p] != strs[i]:
return False
d = dict()
for i, p in enumerate(strs):
if p not in d:
d[p] = pattern[i]
else:
if d[p] != pattern[i]:
return False
return True
日期
2017 年 5 月 19 日
2018 年 11 月 24 日 —— 周六快乐
【LeetCode】290. Word Pattern 解题报告(Python)的更多相关文章
- [LeetCode] 290. Word Pattern 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- [LeetCode] 290. Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- Java [Leetcode 290]Word Pattern
题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- LeetCode 290. Word Pattern (词语模式)
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode 290 Word Pattern
Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- Leetcode 290 Word Pattern STL
Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...
- leetcode 290 Word Pattern(map的应用)
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
随机推荐
- R语言与医学统计图形【7】低级绘图函数
R语言基础绘图系统 基础绘图包之低级绘图函数--气泡图.一页多图.背景网格.添加线条和散点.数学表达式 4.气泡图 symbols是高级绘图函数,可在图上添加标记,标记的形状包括:circles,sq ...
- 2020终于解决Chrome浏览器“崩溃啦”的问题!
Google的chrome莫名其妙突然所有页面都显示"喔唷 崩溃啦",各种插件在右下角弹出报错!这个问题我之前遇到过一次,后来通过改快捷方式的名字解决了.可是这次,隔离回来上班,打 ...
- C++面试基础篇(一)
1. static关键字的作用 (1)全局静态变量 在全局变量前面加上关键字static, 全局变量就定义为一个全局静态变量 在静态存储区,在整个程序运行期间一致存在. 初始化:未初始化的全局静态变量 ...
- tensorboard 拒绝连接无法打开相应页面
启动tensorboard时没有报错,但打开页面却拒绝连接. 解决方法:tensorboard --logdir=TEC4FN --host=127.0.0.1 在命令最后添加 --host=127. ...
- C语言大小端判定
要判定大小端?需要弄清以下几个问题: 1.当一个变量占多个字节时,变量的指针指向的是低地址 2.什么是大小端? 大端模式:是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中. 小 ...
- nodeJs-Stream接口
JavaScript 标准参考教程(alpha) 草稿二:Node.js Stream接口 GitHub TOP Stream接口 来自<JavaScript 标准参考教程(alpha)> ...
- 2021广东工业大学新生赛决赛 L-歪脖子树下的灯
题目:L-歪脖子树下的灯_2021年广东工业大学第11届腾讯杯新生程序设计竞赛(同步赛) (nowcoder.com) 比赛的时候没往dp这方面想(因为之前初赛和月赛数学题太多了啊),因此只往组合数学 ...
- c++string转const char*与char*
#include <iostream> #include <string> #include <memory> using namespace std; const ...
- DP-Burst Balloons
leetcode312: https://leetcode.com/problems/burst-balloons/#/description Given n balloons, indexed fr ...
- JavaIO——File类
1.File文件类 File类(描述具体文件或文件夹的类):是唯一一个与文件本身操作有关的程序类,可完成文件的创建.删除.取得文件信息等操作.但不能对文件的内容进行修改. (1)File类的基本使用 ...