Word Pattern | & II
Word Pattern |
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:
pattern
contains only lowercase alphabetical letters, andstr
contains words separated by a single space. Each word instr
contains only lowercase alphabetical letters.- Both
pattern
andstr
do not have leading or trailing spaces. - Each letter in
pattern
must map to a word with length that is at least 1.
solution:
Split the string, and add the pair to hashmap, if the existing pattern in the hashmap doesn't match the current one, return false.
public boolean wordPattern(String pattern, String str) {
String[] strs = str.split(" ");
if (pattern.length() != strs.length) return false;
Map<Character, String> map = new HashMap<Character, String>();
for (int i = ; i < pattern.length(); i++) {
if (!map.containsKey(pattern.charAt(i))) {
if (map.containsValue(strs[i])) return false;
map.put(pattern.charAt(i), strs[i]);
} else {
if (!strs[i].equals(map.get(pattern.charAt(i)))) return false;
}
}
return true;
}
Word Pattern II
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 substring in str
.
Examples:
- pattern =
"abab"
, str ="redblueredblue"
should return true. - pattern =
"aaaa"
, str ="asdasdasdasd"
should return true. - pattern =
"aabb"
, str ="xyzabcxzyabc"
should return false.
- pattern =
Notes:
You may assume both pattern
and str
contains only lowercase letters.
分析:
As we don't know the breaking point in str, so we have to try one by one. Onc both the pattern string and str string are empty at the same time, it means the pattern we used is correct.
public boolean wordPatternMatch(String pattern, String str) {
return getMapping(pattern, str, new HashMap<Character, String>());
} public boolean getMapping(String pattern, String str, HashMap<Character, String> mapping) {
if (pattern.isEmpty() && str.isEmpty()) {
return true;
} else if (pattern.isEmpty() || str.isEmpty()) {
return false;
} if (mapping.containsKey(pattern.charAt())) {
String map = mapping.get(pattern.charAt());
if (str.length() >= map.length() && str.substring(, map.length()).equals(map)) {
if (getMapping(pattern.substring(), str.substring(map.length()), mapping)) {
return true;
}
}
} else {
for (int i = ; i <= str.length(); i++) { // try each pattern
String p = str.substring(, i);
if (mapping.containsValue(p)) continue; // the upper if condition is its opposite
mapping.put(pattern.charAt(), p);
if (getMapping(pattern.substring(), str.substring(i), mapping)) {
return true;
}
mapping.remove(pattern.charAt());
}
}
return false;
}
Word Pattern | & II的更多相关文章
- Word Pattern II 解答
Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- 291. Word Pattern II
题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- 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] 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: Word Pattern II
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [Swift]LeetCode291. 单词模式 II $ Word Pattern II
Given a pattern and a string str, find if strfollows the same pattern. Here follow means a full matc ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
随机推荐
- html5_canvas-记忆力卡片游戏
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- myeclipse-建立webservice服务端和客户端
一.建立webservice服务端: 1.新建一个web service project,名称为webservice_server截图如下,点击finish. 2.选择工程,点击右键,选择new-&g ...
- Unix/Linux 命令技巧
锁定一个文件夹 为了我的数据隐私,我想要锁定我文件服务器下的/downloads文件夹.因此我运行了: chmod 0000 /downloads root用户仍旧可以访问,而ls和cd命令则不工作. ...
- 18.Android之SharedPreferences数据存储学习
SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...
- codevs1225 八数码难题
题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的A*问题,但是他们不会做,请你帮他们.问题描述 在 3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数 ...
- Android基础类之BaseAdapter
转:http://www.cnblogs.com/mandroid/archive/2011/04/05/2005525.html Android基础类之BaseAdapter BaseAdapter ...
- 千万不要误用 java 中的 HashCode 方法
刚才debug追堆栈的时候发现一个很奇怪的问题 我用IE8和Google的浏览器访问同一个地址 Action的 scope="session" 也设置了 而且两个浏览器提交的参数m ...
- Java NIO原理和使用
Java NIO非堵塞应用通常适用用在I/O读写等方面,我们知道,系统运行的性能瓶颈通常在I/O读写,包括对端口和文件的操作上,过去,在打开一个I/O通道后,read()将一直等待在端口一边读取字节内 ...
- C++中 :: 的意思
表示作用域,和所属关系 ::是运算符中等级最高的,它分为三种:1)global scope(全局作用域符),用法(::name)2)class scope(类作用域符),用法(class::name) ...
- 9个 SSH常用命令选项
9个 SSH常用命令选项 SSH 是什么 SSH(全称 Secure Shell)是一种加密的网络协议.使用该协议的数据将被加密,如果在传输中间数据泄漏,也可以确保没有人能读取出有用信息.要使用 SS ...