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.
Examples:
- pattern =
"abba", str ="dog cat cat dog"should return true. - pattern =
"abba", str ="dog cat cat fish"should return false. - pattern =
"aaaa", str ="dog cat cat dog"should return false. - pattern =
"abba", str ="dog dog dog dog"should return false.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
二者匹配的问题,不是2*2的空不空,而是二者长度是否相等
[思维问题]:
忘了分离单词怎么写了
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
hashmap两次输入的类型不一致,可以不写类型,直接丢掉<>尖括号
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
hashmap存同样的值,返回值不同
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
hashmap存同样的值,返回值不同:
import java.util.HashMap;
import java.util.Map; public class Test {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
String p1 = map.put("11", "22");
System.out.println("p1:" + p1); String p2 = map.put("33", "44");
System.out.println("p2:" + p2); String value1 = map.get("11");
System.out.println("value1:" + value1); String p3 = map.put("11", "44");
System.out.println("p3:" + p3); String value2 = map.get("11");
System.out.println("value2:" + value2);
}
} p1:null
p2:null
value1:22
p3:22
value2:44
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
291. Word Pattern II 没有空格,回溯法?
[代码风格] :
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] 290. Word Pattern 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- 290 Word Pattern 单词模式
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循这种模式.这里的 遵循 指完全匹配,例如在pattern里的每个字母和字符串 str 中的每个非空单词存在双向单映射关系 ...
- 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] 291. Word Pattern II 词语模式 II
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- 【leetcode】290. Word Pattern
problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
- leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- [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] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
随机推荐
- mysql 授予远程连接直接访问
不通过ssh通道,mysql 授予远程连接直接访问 语句 GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '!DSJdg!' WITH GR ...
- 11-THREE.JS 相机始终朝向某个物体
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- grunt-2x2x
a grunt plugin to resize and rename @2x.png(jpg,gif,) image to .png(jpg,gif) 场景:移动前端开发中,设计给的psd都是双倍图 ...
- fiddler抓包
1.fiddler安装 下载exe安装文件,傻瓜式安装 2.fiddler配置 点击Tools -->Telerik Fiddler Options -->Https,进入以下对话框 Ht ...
- 性能测试工具BenchmarkDotnet
.NET Core中的性能测试工具BenchmarkDotnet https://www.cnblogs.com/lwqlun/p/9671611.html 背景介绍 之前一篇博客中,我们讲解.NET ...
- 简洁的Jquery弹出窗插件
做项目时,很多时候都需要弹窗提示.如果要求不是很严格的项目,直接使用alert就可以搞定.对于需要高度定制化的项目,而且要求比较高的时候,就需要设计符合整体风格的弹出层,这种有美工帮忙,也比较好搞定. ...
- LeetCode Valid Palindrome II
原题链接在这里:https://leetcode.com/problems/valid-palindrome-ii/description/ 题目: Given a non-empty string ...
- Yii 利用layer删除数据
一.视图 <tr id="rm_<?php echo $v->category_id;?>"> <td><?php echo $v- ...
- unity3d___UGui中如何创建loading...进度条
http://blog.sina.com.cn/s/blog_e82e8c390102wh2z.html 实现方法:通过Image组件中Image Type属性中Fill Amount,通过代码改变F ...
- Android手机里的垃圾文件和文件夹清理
SD卡中各个文件夹功能的最详尽分析SD卡用久了会有好多文件夹出现,大家看看都是干什么用~ 1..android_secure 是官方app2sd的产物,删了之后装到sd卡中的软件就无法使用了.2.. ...