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:
patterncontains only lowercase alphabetical letters, andstrcontains words separated by a single space. Each word instrcontains only lowercase alphabetical letters.- Both
patternandstrdo not have leading or trailing spaces. - Each letter in
patternmust 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 ...
随机推荐
- java核心数据结构总结
JDK提供了一组主要的数据结构的实现,如List.Set.Map等常用结构,这些结构都继承自java.util.collection接口. List接口 List有三种不同的实现,ArrayList和 ...
- js实现开灯关灯效果
<!DOCTYPE html> <html> <body> <script> function changeImage() { element=docu ...
- Spring AOP详解 、 JDK动态代理、CGLib动态代理
AOP是Aspect Oriented Programing的简称,面向切面编程.AOP适合于那些具有横切逻辑的应用:如性能监测,访问控制,事务管理以及日志记录.AOP将这些分散在各个业务逻辑中的代码 ...
- BIEE 后台新建分析没有你创建的数据源
(1)登录http://win-5rnnibkasrt:9704/analytics/saw.dll?bieehome 点击“管理” 找到“发出SQL语句”在里面写call sapurgeallca ...
- BZOJ-1968 COMMON 约数研究 数论+奇怪的姿势
1968: [Ahoi2005]COMMON 约数研究 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1513 Solved: 1154 [Submit] ...
- 【蒟蒻の进阶PLAN】 置顶+持续连载
看到周围神犇们纷纷列计划,本蒟蒻也决定跟随他们的步伐,计划大约是周计划吧,具体怎么安排我也不确定.. 2015.12.30 刚刚学习完最基础的网络流,需要进行这方面的练习,从简到难,有空余的话尝试学习 ...
- UVa 12505 Searching in sqrt(n)
传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且 ...
- android HDMI 清晰度 分辨率
但改变分辨率时,发送广播即可: Intent intent_outputmode_change = new Intent(ACTION_OUTPUTMODE_CHANGE); intent_o ...
- 在 ASP.NET MVC 3 中应用 KindEditor
http://www.cnblogs.com/weicong/archive/2012/03/31/2427608.html 第一步 将 KindEditor 的源文件添加到项目中,建议放到 /Scr ...
- serialization机制
首先说明一下序列化的知识: java中的序列化(serialization)机制能够将一个实例对象的状态信息写入到一个字节流中,使其可以通过socket进行传输.或者持久化存储到数据库或文件系统中:然 ...