leetcode题解-205.Isomorphic Strings && 290. Word Pattern
2016-07-19 14:45
417 查看
题目:Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given “egg”, “add”, return true.
Given “foo”, “bar”, return false.
Given “paper”, “title”, return true.
Note: You may assume both s and t have the same length.
其实就是看两个单词结构是否相同。将一个单词中字母替换能否得到另一个单词。
思路一:使用HashMap存储对应的替换关系。
思路二:使用数组,数组的效率普遍比哈希表快。因为一个是下标查找,一个是使用哈希值查找。
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.
这道题与上一题有着类似的思路,使用hashmap来存储pattern与str之间的映射关系即可。注意这里使用tmp.containsValue(ss[i])来判断双向映射条件是否满足,以防止出现上面第四个例子被漏判。
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given “egg”, “add”, return true.
Given “foo”, “bar”, return false.
Given “paper”, “title”, return true.
Note: You may assume both s and t have the same length.
其实就是看两个单词结构是否相同。将一个单词中字母替换能否得到另一个单词。
思路一:使用HashMap存储对应的替换关系。
public boolean isIsomorphic(String s, String t) { if(s == null || s.length() <= 1) return true; HashMap<Character, Character> map = new HashMap<Character, Character>(); for(int i = 0 ; i< s.length(); i++){ char a = s.charAt(i); char b = t.charAt(i); if(map.containsKey(a)){ if(map.get(a).equals(b)) continue; else return false; }else{ if(!map.containsValue(b)) map.put(a,b); else return false; } } return true; }
思路二:使用数组,数组的效率普遍比哈希表快。因为一个是下标查找,一个是使用哈希值查找。
public boolean isIsomorphic2(String s1, String s2) { int[] m = new int[512]; for (int i = 0; i < s1.length(); i++) { if (m[s1.charAt(i)] != m[s2.charAt(i)+256]) return false; m[s1.charAt(i)] = m[s2.charAt(i)+256] = i+1; } return true; }
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.
这道题与上一题有着类似的思路,使用hashmap来存储pattern与str之间的映射关系即可。注意这里使用tmp.containsValue(ss[i])来判断双向映射条件是否满足,以防止出现上面第四个例子被漏判。
public boolean wordPattern(String pattern, String str) { Map<Character, String> tmp = new HashMap<>(); String [] ss = str.split(" "); if(pattern.length() != ss.length) return false; for(int i=0; i<pattern.length(); i++) { if(tmp.containsKey(pattern.charAt(i))){ if(! tmp.get(pattern.charAt(i)).equals(ss[i])) return false; }else if(tmp.containsValue(ss[i])) return false; else tmp.put(pattern.charAt(i), ss[i]); } return true; }
相关文章推荐
- 八大排序算法
- valid Palindrome -- leetcode
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- 用mvn test运行test case时,报“java.lang.OutOfMemoryError: PermGen space”
- ORACLE 查看RMAN的备份信息总结
- Java动态代理(二)CGLIB动态代理应用
- springmvc3+hibernate3整合问题
- Voice Lab 8-SIP笔记
- 深入浅出ClassLoader
- 农业垂直搜索引擎七》web模块的设计与实现
- forward内部跳转 和redirect重定向跳转的区别与共同点
- 《设计模式》之Creational模式:Builder
- TCP版本的nginx编译
- c++面试关键字整理
- javascript数据类型理解整理
- 如何在阿里云服务器里配置iis 搭建web服务
- Android逆向分析案例——某地图的定位请求分析
- Web---演示servlet技术(servlet生命周期),解决中文乱码问题
- 推荐个汇编语言编译器 XP系统
- 面试题:TCP协议与UDP协议的区别