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 ...
随机推荐
- Windows10 家庭版没有本地组策略解决方法
windows 家庭版默认是没有本地组策略的, win+R运行gpedit.msc提示不存在 下面的代码保存一个cmdorbat 脚本文件add_gpedit.cmd, 并执行 add_gpedit. ...
- extern “C”的作用详解
extern "C"的主要作用就是为了能够正确实现C++代码调用其他C语言代码.加上extern "C"后,会指示编译器这部分代码按C语言的进行编译,而不是C+ ...
- MySQL面试题36道
MySQL数据库是在免费的数据库中最受欢迎的一款,尤其是在一些小型项目以及项目资金有限的情况下,选择MySQL来作为数据存储的工具,那些不差钱并且数据吞吐量非常大的互联网公司一般都是会用付费的Orac ...
- KMS服务器软件-windows/OpenWRT-X64版
软件项目: https://github.com/Wind4/vlmcsd windows版kms服务器 https://files.cnblogs.com/files/SilenceRet/vlmc ...
- PTA-B 1039 到底买不买 解题思路记录
#include <cstdio> #include <string> #include <iostream> using namespace std; int m ...
- 张高兴的 .NET Core IoT 入门指南:(二)GPIO 的使用
什么是 GPIO GPIO 是 General Purpose Input Output 的缩写,即"通用输入输出". Raspberry Pi 有两行 GPIO 引脚, Rasp ...
- PyQt5--Position
# -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...
- 让sublime text3支持Vue语法高亮显示[转]
1.准备语法高亮插件vue-syntax-highlight. 下载地址:https://github.com/vuejs/vue-syntax-highlight 下载页面并下载: 解开压缩包vue ...
- Oracle RMAN 恢复数据库到不同主机(一)
一.RMAN 备份的内容 RMAN做数据库全备时包含了 数据文件.归档日志.控制文件和参数文件和备份日志,如下: arch_20160223_08qukp2t_1_1 arch_20160223_0 ...
- eclipse中ant打war包
注:有可能打包失败是eclipse没有集成的原因,用我自己的eclipse可以,同事那就有可能打包失败,一定要注意这点. 1.先在eclipse中集成或自己安装解压ant,总之有可用的ant就好. 2 ...