LeetCode_290. Word Pattern
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
.
Example 1:
Input: pattern ="abba"
, str ="dog cat cat dog"
Output: true
Example 2:
Input:pattern ="abba"
, str ="dog cat cat fish"
Output: false
Example 3:
Input: pattern ="aaaa"
, str ="dog cat cat dog"
Output: false
Example 4:
Input: pattern ="abba"
, str ="dog dog dog dog"
Output: false
Notes:
You may assume pattern
contains only lowercase letters, and str
contains lowercase letters that may be separated by a single space.
package leetcode.easy; public class WordPattern {
@org.junit.Test
public void test() {
String pattern1 = "abba";
String str1 = "dog cat cat dog";
String pattern2 = "abba";
String str2 = "dog cat cat fish";
String pattern3 = "aaaa";
String str3 = "dog cat cat dog";
String pattern4 = "abba";
String str4 = "dog dog dog dog";
System.out.println(wordPattern(pattern1, str1));
System.out.println(wordPattern(pattern2, str2));
System.out.println(wordPattern(pattern3, str3));
System.out.println(wordPattern(pattern4, str4));
} public boolean wordPattern(String pattern, String str) {
String[] arrays = str.split(" ");
if (arrays.length != pattern.length()) {
return false;
} java.util.Map<String, Character> map = new java.util.HashMap<>();
for (int i = 0; i < pattern.length(); i++) {
if (map.containsKey(arrays[i]) && map.get(arrays[i]) != pattern.charAt(i)) {
return false;
} else if (!map.containsKey(arrays[i]) && map.containsValue(pattern.charAt(i))) {
return false;
} else {
map.put(arrays[i], pattern.charAt(i));
}
}
return true;
}
}
LeetCode_290. Word Pattern的更多相关文章
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- [LeetCode] Word Pattern
Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...
- Word Pattern | & II
Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...
- 291. Word Pattern II
题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- Word Pattern
package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** * * @au ...
- Word Pattern II 解答
Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- 【leetcode】290. Word Pattern
problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
随机推荐
- django 时间格式(全局修改,不用过滤器)
百度了一圈,很没创意的用过滤器,前端每次显示时间表格都要用过滤器,这种挺烦的.隐约记得以前见过没有用过滤器的.换google https://stackoverflow.com/questions/5 ...
- YAML_07 有报错信息,告诉你错误忽略,继续执行下面的命令
ansible]# vim user5.yml --- - hosts: cache remote_user: root vars: user: bb tasks: - sh ...
- webuploader+Java如何实现分片+断点续传
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...
- Ubuntu下彻底卸载默认安装的mysql,自己手动下载安装MYSQL
彻底卸载: sudo apt-get autoremove --purge mysql-server-5.7 sudo apt-get remove mysql-common sudo rm -rf ...
- ssm + ehcache 运行异常
异常: DEBUG [net.sf.ehcache.CacheManager@295af581] - Checking for update...DEBUG [net.sf.ehcache.Cache ...
- 微信小程序 图片设置为圆形
要图片圆形显示,需要设置border-radius:50%,还要设置overflow:hidden,具体如下: Tip:user-avatar是图片控件的class .user-avatar { wi ...
- 2018-2019-2 网络对抗技术 20165322 Exp7 网络欺诈防范
2018-2019-2 网络对抗技术 20165322 Exp7 网络欺诈防范 目录 实验原理 实验内容与步骤 简单应用SET工具建立冒名网站 ettercap DNS spoof 结合应用两种技术, ...
- ubuntu之路——day1(一点十五分 MMP终于把显卡装好了)
因为要上手深度学习的原因,购置了一台RTX2080TI+ubuntu18.04的机器 例行两条命令 sudo apt-get update sudo apt-get upgrade 开启巨坑第一天,以 ...
- Perl深度优先迷宫算法
迷宫求解,可以用穷举法,将每个点的方向都穷举完:由于在求解过程中会遇到某一方向不可通过,此时就必须按原路返回. 想到用Perl数组来保存路径,记录每次所探索的方向,方便原路返回时得到上一步的方向,再退 ...
- 范仁义web前端介绍课程---4、html、css、js初体验
范仁义web前端介绍课程---4.html.css.js初体验 一.总结 一句话总结: html:就是网站的骨架,比如div标签.a标签等 css:style标签或者style属性里面的就是css j ...