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 ...
随机推荐
- 图片延迟加载scrollLoading.js应用
<ul> <li><a href="http://news.qq.com/" target="_b ...
- hdu 5429 Geometric Progression(存个大数模板)
Problem Description Determine whether a sequence is a Geometric progression or not. In mathematics, ...
- sql语句中BEGIN TRAN...COMMIT TRAN
BEGIN TRAN标记事务開始 COMMIT TRAN 提交事务 一般把DML语句(select ,delete,update,insert语句)放在BEGIN TRAN...COMMIT TR ...
- EffectiveC#16--垃圾最小化
1.申请和释放一个基于堆内存的对象要花上更多的处理器时间. 所以当一个引用类型的局部变量在常规的函数调用中使用的非常频繁时应该把它提升为对象的成员(方法一) 2.当你把一个实现了IDisposable ...
- jquery selector
jquery的选择器功能 1 :lt(index) selector 一组元素选择index之前的元素,若index<0 则倒着选过来 http://api.jquery.com/lt-sele ...
- Android WebView 软键盘挡住输入框
解决方法一: 在所在的Activity中加入 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RES ...
- 企业qq代码,工作中用到的
<div id="xixi" onmouseover="toBig()" style="top: 120px; left: 0; positio ...
- C#图像处理(1):在图片上加文字和改变文字的方向
C#在图片上加文字,代码如下: /// <summary> /// 图片上方加文字,文字将会被180度反转 /// </summary> /// <param name= ...
- Xcode6和Xcode5获取app名字
1.在Xcode5下,获取程序名字(app name)的方法为: NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionar ...
- NSURL
1. NSURL的简介 URL是对可以从互联网上得到的资源的位置和访问方法的一种简介的表示,是互联网上标准资源的地址.URL可能包含远程服务器上的资源位置,本地磁盘上的文件的路径,甚至任意一段编码的数 ...