作者: 负雪明烛
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)的更多相关文章

  1. [LeetCode] 290. Word Pattern 单词模式

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

  2. leetcode 290. Word Pattern 、lintcode 829. Word Pattern II

    290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...

  3. [LeetCode] 290. Word Pattern 词语模式

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

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

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

  5. Java [Leetcode 290]Word Pattern

    题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

  6. LeetCode 290. Word Pattern (词语模式)

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

  7. LeetCode 290 Word Pattern

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

  8. Leetcode 290 Word Pattern STL

    Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...

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

随机推荐

  1. Python——MacBook Pro中安装pip

    1.系统已有python2和python3,如何检查MacBook Pro系统是否安装的有pip? 看到terminal的提示没有,有提示pip的,下面的提示,说明pip安装了. 要查看pip3是否安 ...

  2. Web网页服务器软件——介绍

    Web网页服务器软件与硬件服务器的关系,就像软件和电脑的关系. 目前有,世界使用排列第一名的Apache.还有可以在Linux系统下快速方便地搭建出LNMP Web服务环境的Nginx(其中LNMP分 ...

  3. Linux之crond任务调度

    1. 示意图 2. 基本语法 crontab [选项] # -e : 编辑crontab定时任务 # -l : 查询crontab # -r : 删除当前用户所有的crontab任务 # 例子: # ...

  4. Linux升级命令yum upgrade和yum update的区别

    Linux升级命令有两个分别是yum upgrade和yum update, 这个两个命令是有区别的: yum -y update 升级所有包同时也升级软件和系统内核 yum -y upgrade 只 ...

  5. [源码解析] PyTorch 分布式 Autograd (5) ---- 引擎(上)

    [源码解析] PyTorch 分布式 Autograd (5) ---- 引擎(上) 目录 [源码解析] PyTorch 分布式 Autograd (5) ---- 引擎(上) 0x00 摘要 0x0 ...

  6. 基于Kubernetes实现前后端应用的金丝雀发布

    基于Kubernetes实现前后端应用的金丝雀发布 公司的研发管理平台实现了Gitlab+Kubernetes的Devops,在ToB和ToC场景中,由于用户量大,且预发布环境和生产环境或多或少存在差 ...

  7. 用原生CSS编写-怦怦跳的心

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. Python计算期权隐含波动率

    更多精彩内容,欢迎关注公众号:数量技术宅,也可添加技术宅个人微信号:sljsz01,与我交流. Black-Scholes 将期权价格描述为标的价格.行权价.无风险利率.到期时间和波动性的函数.  V ...

  9. React 16.13.1触发两次render

    一段很普通的代码,出发了两次render import React, { useState, useEffect } from 'react' const MouseTracker: React.FC ...

  10. 【leetcode】563. Binary Tree Tilt

    Given the root of a binary tree, return the sum of every tree node's tilt. The tilt of a tree node i ...