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 ...
随机推荐
- MySQL递归查询父节点或递归查询子节点-陈远波
根据id查询父节点,具体需要修改的地方笔者已在注释中给大家作了注解 DELIMITER $$ USE `yjlc_platform`$$ -- getCompanyParent 为函数名 DROP F ...
- [python]如何理解uiautomator里面的 instance 及使用场景
通过uiautomatorviewer打开之后,需要通过对某个控件进行操作,但在当前界面中该控件所有属性无法唯一(其它控件属性也是一样),这个时候就需要借助实例(instance)来进行区分,inst ...
- 启动报错:Access denied for user 'root'@'localhost' (using password:YES)
项目启动报错:Access denied for user 'root'@'localhost' (using password:YES) 原因:root帐户默认不开放远程访问权限,所以需要修改一下相 ...
- WorldWind源码剖析系列:下载队列类DownloadQueue
下载队列类DownloadQueue代表具有优先级的下载队列,该类的存储下载请求的数组链表专门按一定的优先级来存储下载请求的.该类的类图如下. 下载队列类DownloadQueue各个字段的含义说明如 ...
- jmeter接口测试4-使用数据库mysql构造参数
jmeter测试中,测试数据一般和测试用例分离 测试数据一般可以使用csv构造,进行参数化 但也可以使用mysql等数据库构造 方案一:一个线程循环调用mysql数据,不是并发,不适用于性能测试,更适 ...
- python+requests实现接口测试 - get与post请求使用(转载)
转自:http://www.cnblogs.com/nizhihong/p/6567928.html 简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Lic ...
- Python2.7-struct模块
struct模块 处理二进制数据,与C语言交互,可以较为方便的对C语言的struct类型和python中的数据进行转换 主要是用于将int,char之类的C语言中基础数据pack至一个二进制流的字符串 ...
- 静态工厂方法和实例工厂方法及普通的bean
容纳你的bean bean工厂:最简单的容器,提供了基础的依赖注入支持.创建各种类型的Bean. 应用上下文(ApplicationContext):建立在bean工厂基础之上,提供系统架构服务. ...
- openJDK环境搭建编译(fedora)
1.安装VMware VMware-workstation-full-10.0.7-2844087.exe 破解码:HY06L-F334P-9Z6H9-6R2XM-23C6J 安装完成之后, ...
- JAVA框架 Mybaits 核心配置
一:mybaits的核心配置文件:SqlMapConfig.xml 配置文件中需要关注的属性: 二.properites属性:一般引用配置文件(properites文件)比如:数据库的配置.我们可以编 ...