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:
- 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.
链接: http://leetcode.com/problems/word-pattern/
1 public class Solution { 2 public boolean wordPattern(String pattern, String str) { 3 String[] parts = str.split(" "); 4 if (pattern.length() != parts.length) return false; 5 HashMap<Character, String> hcs = new HashMap<Character, String>(); 6 HashMap<String, Character> hsc = new HashMap<String, Character>(); 7 8 for(int i = 0; i < parts.length; i++) { 9 if (!hsc.containsKey(parts[i]) && !hcs.containsKey(pattern.charAt(i))) { 10 hsc.put(parts[i], pattern.charAt(i)); 11 hcs.put(pattern.charAt(i), parts[i]); 12 } else if (hsc.containsKey(parts[i]) && hcs.containsKey(pattern.charAt(i)) && hsc.get(parts[i]) == pattern.charAt(i) && hcs.get(pattern.charAt(i)).equals(parts[i])) { 13 continue; 14 } else { 15 return false; 16 } 17 } 18 return true; 19 } 20 }
转载于:https://www.cnblogs.com/panini/p/6517647.html
290. Word Pattern相关推荐
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
- 290. Word Pattern
/** 290. Word Pattern* 2016-7-2 by Mingyang* 这里加上了没有containsValue,因为这里如果abba 和 dog dog dog dog通不过,* ...
- [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
题目链接:https://leetcode.com/problems/word-pattern/ 思路分析:题目要求判断在pattern中的字符与str中的非空word之间是否存在双射,解法如代码所示 ...
- 《每日一题》290. Word Pattern
给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循相同的规律. 这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向 ...
- C#LeetCode刷题之#290-单词模式(Word Pattern)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3778 访问. 给定一种 pattern(模式) 和一个字符串 s ...
- LeetCode : 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 Patter
解题思路:先将字符串使用string流的形式分成一个个字符串,之后,将pattern和str 分别存到一个map容器中,并比较出现的次数是否相等. class Solution { public:bo ...
- 查找算法常见的五大面试知识点与两类实战!
↑↑↑关注后"星标"Datawhale 每日干货 & 每月组队学习,不错过 Datawhale干货 作者:周郴莲,东北石油大学,Datawhale优秀学习者 前言 查找(S ...
最新文章
- 机器人过程自动化的10个秘密
- 金立M2017续航出色不仅是因为电池大,还有超强快充
- java实现linkstring,【JAVA SE基础篇】32.String类入门
- WPF学习之路(六)Command
- HTTP Error 415: Unsupported Media Type! 这个错误
- 操作系统——Linux 虚拟内存和物理内存的理解
- 教你轻松解决CSRF跨站请求伪造攻击
- 电影推荐算法及python实现
- oracle怎么加上双引号,Oracle中的双引号的作用
- C#递归算法使用案例——画树
- Android中MVP框架理解
- 小程序源码:uni-app云开发的网盘助手
- 弘辽科技:淘宝收藏加购在哪里看?如何提升收藏加购数量?
- 创建线程的三种方式、线程运行原理、常见方法、线程状态
- Word中去掉各种标记符号
- SpringBoot+Thymeleaf实现图片上传和显示
- 墨客和宁波市政府签订战略合作协议
- 【编程小技巧】实现弹窗、选项、关机(文件后缀改成.vbs)
- ​小城故事—逃离之路
- beast附加包安装:SNAPP安装出现问题的解决办法