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的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
随机推荐
- Java - Annotation使用
本文转载于(这个写的很好):https://www.cnblogs.com/be-forward-to-help-others/p/6846821.html Annotation Annotation ...
- Spark运行架构及作业提交流程
1.yarn-cluster模式: (1)client客户端提交spark Application应用程序到yarn集群. (2)ResourceManager收到了请求后,在集群中选择一个NodeM ...
- MySQL 索引原理以及慢查询优化
本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BTree ...
- MCMC蒙特卡罗马尔科夫模型
https://www.cnblogs.com/pinard/p/6645766.html https://blog.csdn.net/saltriver/article/details/521949 ...
- SignalR2实时聊天
SignalR2实时聊天 NuGet包中搜索SignalR添加引用 using Microsoft.AspNet.SignalR; 创建OWIN启动类 namespace SignalRChat { ...
- bzoj 1072: [SCOI2007]排列perm 状压dp
code: #include <bits/stdc++.h> #define N 1005 using namespace std; void setIO(string s) { stri ...
- Session的数据共享
要体现出Session的数据共享,需要建立两个Servlet: 第一个:建立Session,将值设置为Tom. protected void doGet(HttpServletRequest requ ...
- bg/fg/jobs
用于将某个任务放置后台运行,一般会与 ctrl+ z , fg, & 符号联用. 典型的场景就是将耗时的任务放于后台运行,例如打包某个占用空间大的目录,
- C# 跨域 请求带cookie
原文:https://blog.csdn.net/z69183787/article/details/78954325 背景: 别个的项目,要开发App接口,要求用前端AJAX的方式访问接口数据. 后 ...
- Linux下搭建iSCSI共享存储的方法 TGT 方式 Debian9.5系统下
iSCSI(internet SCSI)技术由IBM公司研究开发,是一个供硬件设备使用的.可以在IP协议的上层运行的SCSI指令集,这种指令集合可以实现在IP网络上运行SCSI协议,使其能够在诸如高速 ...