Isomorphic Strings 解答
Question
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.
Solution 1
Use hashmap, remember to check both key and value. Time complexity O(n^2), space cost O(n).
hashMap.containsValue costs O(n)
public class Solution {
public boolean isIsomorphic(String s, String t) {
if (s == null || t == null)
return false;
if (s.length() != t.length())
return false;
if(s.length()==0 && t.length()==0)
return true;
int length = s.length();
Map<Character, Character> map = new HashMap<Character, Character>();
for (int i = 0; i < length; i++) {
char tmp1 = s.charAt(i);
char tmp2 = t.charAt(i);
if (map.containsKey(tmp1)) {
if (map.get(tmp1) != tmp2)
return false;
} else if (map.containsValue(tmp2)) {
return false;
} else {
map.put(tmp1, tmp2);
}
}
return true;
}
}
Solution 2
Use extra space to reduce time complexity.
public class Solution {
public boolean isIsomorphic(String s, String t) {
if (s == null || t == null)
return false;
if (s.length() != t.length())
return false;
if(s.length()==0 && t.length()==0)
return true;
int length = s.length();
Map<Character, Character> map = new HashMap<Character, Character>();
Set<Character> counts = new HashSet<Character>();
for (int i = 0; i < length; i++) {
char tmp1 = s.charAt(i);
char tmp2 = t.charAt(i);
if (map.containsKey(tmp1)) {
if (map.get(tmp1) != tmp2)
return false;
} else if (counts.contains(tmp2)) {
return false;
} else {
map.put(tmp1, tmp2);
counts.add(tmp2);
}
}
return true;
}
}
Isomorphic Strings 解答的更多相关文章
- [LeetCode] Isomorphic Strings
Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...
- leetcode:Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Codeforces 985 F - Isomorphic Strings
F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代 ...
- Educational Codeforces Round 44 (Rated for Div. 2) F - Isomorphic Strings
F - Isomorphic Strings 题目大意:给你一个长度为n 由小写字母组成的字符串,有m个询问, 每个询问给你两个区间, 问你xi,yi能不能形成映射关系. 思路:这个题意好难懂啊... ...
- CodeForces985F -- Isomorphic Strings
F. Isomorphic Strings time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- LeetCode_205. Isomorphic Strings
205. Isomorphic Strings Easy Given two strings s and t, determine if they are isomorphic. Two string ...
随机推荐
- hihoCoder 1092 : Have Lunch Together
题目大意:小hi和小ho去咖啡厅喝咖啡,咖啡厅可以看作是n * m的矩阵,每个点要么为空,要么被人.障碍物.椅子所占据,小hi和小ho想要找两个相邻的椅子.起初两个人都在同一个点,求两人到达满足要求的 ...
- UESTC_Dividing Numbers CDOJ 1156
Dividing Numbers Time Limit: 9000/3000MS (Java/Others) Memory Limit: 262144/262144KB (Java/Other ...
- POJ3261-哈希
这个题让求至少出现K次的最大长度的子串,属于最大化最小值问题,首先应该想到二分求字串的长度,二分的过程是O(logN)的,注意judge的时候怎样判断是否满足情况以及满足情况后l,r的变化.可以给每一 ...
- poj2787 算24
每次枚举两个数,一个运算符,得到一个运算结果.在重复执行,直到只剩下一个数即可. #include <iostream> #include <cmath> using name ...
- Live555 分析(一):类介绍
从程序的结构来看,live项目包括了四个基本库.程序入口类(在mediaServer中)和一些测试代码(在testProgs中). 四个基本静态库是UsageEnvironment.BasicUsag ...
- MyCat部署运行(Windows环境)与使用步骤详解
目录(?)[+] 1.MyCat概念 1.1 总体架构 MyCAT的架构如下图所示: MyCAT使用MySQL的通讯协议模拟成一个MySQL服务器,并建立了完整的Schema(数据库).Tab ...
- iOS tableView的图片缓存异步载入
1.建立一个viewController. .h文件实现UIScrollViewDelegate和UITableViewDelegate,并声明ICTableViewDelegate(用来实现图片有缓 ...
- 对于Android Service 生命周期进行全解析
应用程序组件有一个生命周期——一开始Android实例化他们响应意图,直到结束实例被销毁.在这期间,他们有时候处于激活状态,有时候处于非激 活状态:对于活动,对用户有时候可见,有时候不可见.组件生命周 ...
- C#软件开发实例.私人订制自己的屏幕截图工具(七)加入放大镜的功能
上一篇:C#软件开发实例.私人订制自己的屏幕截图工具(六)加入配置管理功能 因为截图时可能须要精确截取某一部分,所以须要放大镜的功能,这样截取的时候才更easy定位截图的位置. 加入PictureBo ...
- windows-JDK环境变量设置
JAVA_HOME=C:\Program Files\Java\jdk1.6.0_43;CLASS_PATH=.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar;p ...