290. Word Pattern【LeetCode by java】
今天发现LintCode页面刷新不出来了,所以就转战LeetCode。还是像以前一样,做题顺序:难度从低到高,每天至少一题。
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 separated by a single space.
解题:题目给定俩字符串,第一个字符串是由若干非空字符组成,第二个字符串由若干单词组成,用空白字符隔开。要求判断两组字符串格式是否相同。先贴一下自己写的代码吧,写的代码比较多,也没啥技术含量。最主要的思想是,遍历第一个字符串,比较各个位置上的字符是否相同,同时比较另一个字符串相同位置的单词是否相等,如果有不匹配的,返回false,遍历结束时,返回true。代码如下:
class Solution {
public boolean wordPattern(String pattern, String str) {
if(pattern == null && str == null)
return true;
String []temp = str.split(" ");
if(pattern.length() != temp.length)
return false;
if(pattern.length() ==1)
return true;
for(int i = 1; i < pattern.length(); i++){
for(int j = 0; j < i; j++){
if(pattern.charAt(i) == pattern.charAt(j)){
if(!equal(temp, i, j)){
return false;
}
}
if(pattern.charAt(i) != pattern.charAt(j)){
if(equal(temp, i, j)){
return false;
}
}
}
}
return true;
}
public boolean equal(String[]temp,int index1,int index2){
if(temp[index1].equals(temp[index2])){
return true;
}else{
return false;
}
}
}
}
在discussion上看到了更好的方法,用hash表来做的。hashmap中插入一组数据的方法是:public V put (K key, V value ) 如果插入一组数据时,已经有key存在,则返回 旧的value,并用新的value来覆盖旧的value。
那么用一个循环同时遍历两个String,如果相同位置有重复的,说明两个字符串匹配,反之,如果哪个位置上,一个字符串发现已经有这个key值了,另一个string却发现hashmap里并没有重复出现的key值,说明两个字符串的格式并不匹配。代码如下:
class Solution {
public boolean wordPattern(String pattern, String str) {
String[]words = str.split(" ");
if(pattern.length() != words.length)
return false;
Map map1=new HashMap();
Map map2=new HashMap();
for(int i = 0; i < pattern.length(); i++){
if((map1.put(pattern.charAt(i), i)) != map2.put(words[i], i))
return false;
}
return true;
}
}
由于此题把第二个字符串转化为了字符数组,那么遍历时及时字符和字符串内容相同,其类型也不相同,所以可以只用一个map。代码进一步化简为:
class Solution {
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
if (words.length != pattern.length())
return false;
Map index = new HashMap();
for (Integer i = 0; i < words.length; ++i)
if (index.put(pattern.charAt(i), i) != index.put(words[i], i))
return false;
return true;
}
}
290. Word Pattern【LeetCode by java】的更多相关文章
- 501. Find Mode in Binary Search Tree【LeetCode by java】
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- 7. Reverse Integer【Leetcode by java】
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- 【leetcode】290. Word Pattern
problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
- leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- [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 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
随机推荐
- 微信支付回调,XXE攻击漏洞防止方法
最近微信支付回调发现的XXE攻击漏洞(什么是XXE攻击,度娘.bing去搜,一搜一大把),收到通知后即检查代码, 微信给的解决方法是如果你使用的是: XmlDocument: XmlDocument ...
- Custom Voice 操作步骤
首先,准备数据 1.Unicode格式的Transcript 2wav格式语音数据,并打包 好,现在POSTMAN进行api测试. 先拿着订阅密钥(Subscription Key)获取令牌(Toke ...
- ASCII码查看
字母对照表: ASCII可显示字符: ASCII控制字符:
- SecureCRT Win免安装版本,简单好用
SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件. 这个简单好用,程序员必备. 下载地址:SecureCRT. ...
- python第二十九课——文件读写(读取数据操作)
演示读取数据操作:path=r'a.txt' 1.打开文件f1=open(path,'r') 2.读取数据content1=f1.read(3)print(content1) content1=f1. ...
- sql 一个表的字段更新至另一个字段的方法
update Lc_Taxs set TaxMember = convert(int,Lc_Taxs2.TaxNo) from Lc_Taxs a,(select * from Lc_Taxs ) ...
- UMI开源项目
本文主要围绕UMI是什么及其特征.安装应用.模板例子等四个方面内容来讲解UMI,希望能够对初学者有所启发. 一. UMI是什么 UMI是可插拔的企业级反应应用程序框架. 二. 特征 特征
- 利用jenkins打造通过自定义参数更新svn 指定文件任务
jenkin可以执行很多构建任务,有时候我们需要在执行构成中同构shell对服务器进行操作而且还需要进行参数的传入 比如:我要利用svn进行本地代码的更新,单又不是所有代码的更新,只更新指定的1个或这 ...
- WorldWind源码剖析系列:表面影像类SurfaceImage
表面影像类SurfaceImage描述星球类(如地球)表面纹理影像.该类的类图如下. 表面影像类SurfaceImage包含的主要的字段.属性和方法如下: string m_ImageFilePath ...
- MySQL(六)创建用户与授权
转载自:MySQL创建用户与授权 目录 一.创建用户 二.授权 三.设置和更改用户密码 四.撤销用户权限 五.删除用户 一.创建用户 命令: CREATE USER 'username'@'host' ...