leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern
1 题目
Given a pattern and a string str, find if str follows the same pattern.
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:
patterncontains only lowercase alphabetical letters, and str contains words separated by a single space. Each word in str contains only lowercase alphabetical letters.
Both pattern and str do not have leading or trailing spaces.
Each letter in pattern must map to a word with length that is at least 1.
接口: public boolean wordPattern(String pattern, String str)
2 思路
题意
通过pattern来匹配单词,很容易想到HashMap思想。
注意
pattern的长度,不对应str拆分单词后的长度。- 一个
pattern字母和唯一一个字符串相互对应。
复杂度: Time:O(n) Space: O(n)
3 代码
public boolean wordPattern(String pattern, String str) {
String[] words = str.split("\\s", -1);
int len = words.length;
if (pattern.length() != len) { // 长度不等直接false,防止后面数组越界
return false;
}
Map<Character, String> map = new HashMap<>(26);
for (int i = 0; i < len; i++) {
Character c = pattern.charAt(i);
if (map.containsKey(c)) {
if (!map.get(c).equals(words[i]))
return false;
} else {
if (map.containsValue(words[i])) { // 字母和字符串一一对应。
return false;
}
map.put(c, words[i]);
}
}
return true;
}
4 总结
简单题,注意细节。一次AC。
5 参考
leetcode面试准备: Word Pattern的更多相关文章
- Leetcode solution 291: Word Pattern II
Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...
- 【leetcode】290. Word Pattern
problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
- 【一天一道LeetCode】#290. Word Pattern
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Baozi Leetcode Solution 290: Word Pattern
Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...
- 【LeetCode】290. Word Pattern 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode算法题-Word Pattern(Java实现)
这是悦乐书的第202次更新,第212篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第68题(顺位题号是290).给定一个模式和一个字符串str,找到str是否完全匹配该模 ...
- LeetCode OJ: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 ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
随机推荐
- SQLSERVER 数据库性能的基本
很久没有写文章了,在系统正式上线之前,DBA一般都要测试一下服务器的性能 比如你有很多的服务器,有些做web服务器,有些做缓存服务器,有些做文件服务器,有些做数据库服务器 做数据库服务器的那台服务器性 ...
- ###Canny边缘检测算子
开源中国. #@date: 2014-06-20 #@author: gerui #@email: forgerui@gmail.com 一.一阶微分边缘算子 1. 一阶微分边缘检测算子也称梯度边缘算 ...
- oc 一些通用函数
1 i= 0,1,2... unichar c = [self characterAtIndex:i]; //取出i这个位置对应的字符 2 拼凑字符串 [NSString stringWithForm ...
- 英语中的 姓氏/Surname
.Chomsky (Belarusian: Хомскі, Russian: Хомский, Ukrainian: Хомський, Hebrew: חומסקי, "from (V ...
- [转]怎样在cmd(命令提示符)下进行复制粘贴操作
原文链接:http://jingyan.baidu.com/article/93f9803fd3a4dde0e46f55f5.html cmd下复制粘贴的快捷操作方式 工具/原料 系统cmd 步骤/方 ...
- Entity Framework 学习笔记(2)
上期回顾:Entity Framework 学习笔记(1) Entity Framework最主要的东西,就是自己创建的.继承于DbContext的类: /// <summary> /// ...
- php常用单词语法
header("Content-type:text/html;charset=utf-8"); 加入数组array_push($ratings_store_cop,$value); ...
- 实现scp自动输入密码(判断yesno选项)
1.apt-get install expect 2.编写shell脚本test.sh #!/usr/bin/expect -f#!/bin/shset password 1spawn scp roo ...
- 用LinqToExcel处理有标题表格的数据
1. 先根据表格标题定义一个类. public class News { public string Title { set; get; } public string Content { set; ...
- DataGridView默认不选中
NurseGridList.CurrentCell = null; NurseGridList.ClearSelection(); Nurs ...