今天发现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】的更多相关文章

  1. 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 ...

  2. 7. Reverse Integer【Leetcode by java】

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  3. 【leetcode】290. Word Pattern

    problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...

  4. leetcode 290. Word Pattern 、lintcode 829. Word Pattern II

    290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...

  5. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  6. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  7. 【LeetCode算法-27】Remove Element

    LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...

  8. [LeetCode] 290. Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  9. [LeetCode] 290. Word Pattern 单词模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

随机推荐

  1. LUA 运算笔记

    循环 比如要实现这样的一个For for(int i=10;i>1;i—) { print(i) } lua的for循环 转换成LUA for i=10,1,-1 do print(i) end ...

  2. python图像处理:pytesseract和PIL

    大概介绍下相关模块的概念: Python-tesseract 是光学字符识别Tesseract OCR引擎的Python封装类.能够读取任何常规的图片文件(JPG, GIF ,PNG , TIFF等) ...

  3. 运行结果出现Process finished with exit code 0

    表示程序正常执行完毕并退出. 可以科普一下exit code,在大部分编程语言中都适用 exit code 0表示程序执行成功,正常退出 exit code 1表示执行过程中遇到了某些问题或者错误,非 ...

  4. Chrome 打印PDF技巧

    Chrome 打印PDF技巧 原文地址:https://github.com/zhongxia245/blog/issues/22 欢迎star 本教程,使用Mac电脑进行演示. 常规的Chrome打 ...

  5. 团队作业——Beta冲刺3

    团队作业--Beta冲刺 冲刺任务安排 杨光海天 今日任务:浏览详情界面的开发 明日任务:浏览详情界面的开发 吴松青 今日任务:与队长一同进行图片详情的开发,接触了一些自己没接触过的知识点并向队友学习 ...

  6. 分布式全局ID生成器设计

    项目是分布式的架构,需要设计一款分布式全局ID,参照了多种方案,博主最后基于snowflake的算法设计了一款自用ID生成器.具有以下优势: 保证分布式场景下生成的ID是全局唯一的 生成的全局ID整体 ...

  7. 死磕nginx系列--使用nginx做负载均衡

    使用nginx做负载均衡的两大模块: upstream 定义负载节点池. location 模块 进行URL匹配. proxy模块 发送请求给upstream定义的节点池. upstream模块解读 ...

  8. C#创建无窗体的应用程序

    示例程序 这是初学C#时困惑了很久才解决的问题,突然想起来拿出来和大家分享. 当初我是这样做的: 1.      在窗体初始化时(构造函数里面),添加一句This.Visible = false; 2 ...

  9. CSS 文本

    CSS 文本属性可定义文本的外观. 通过文本属性,您可以改变文本的颜色.字符间距,对齐文本,装饰文本,对文本进行缩进,等等. 缩进文本 把 Web 页面上的段落的第一行缩进,这是一种最常用的文本格式化 ...

  10. 飞冰ICE

    一. 飞冰目标与愿景(同时也概况飞冰是什么) 飞冰是一套基于 React 的中后台应用解决方案,在阿里巴巴内部,已经有 270 多个来自几乎所有 BU 的项目在使用.经过 2 年的发展,飞冰已经是中后 ...