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 ...
随机推荐
- POJ 1631 Bridging signals DP(最长上升子序列)
最近一直在做<挑战程序设计竞赛>的练习题,感觉好多经典的题,都值得记录. 题意:给你t组数据,每组数组有n个数字,求每组的最长上升子序列的长度. 思路:由于n最大为40000,所以n*n的 ...
- 恢复Linux下被误删除的文件(笔记)
恢复Linux下被误删除的文件 [root@xuegod63 ~]# mount /dev/cdrom /mnt/ 分一个区:sda4 查找:extundelete 分一个区:sda4 [root ...
- ubuntu 右键新建文档
第一步.创建一个空白doc文档. 一定要是空白的,然后保存(另存为)到桌面,注意细节:1.保存到你能找到的地方,最好是桌面,以方便第二步拖动 2.注意下方的 “文件类型”,要选择doc格式. 3.注意 ...
- Kafka测试
准备工作 硬件:笔记本,windows10系统4核8G内存 软件:接口测试工具,以及kafka自带测试工具 影响测试结果配置分析 Borker num.network.thread=3 用于接收并处理 ...
- 关系型数据库遵循ACID规则
事务在英文中是transaction,和现实世界中的交易很类似,它有如下四个特性: 1.A (Atomicity) 原子性原子性很容易理解,也就是说事务里的所有操作要么全部做完,要么都不做,事务成功的 ...
- RESTEasy 3.X Helloworld
最近呢,RESTEasy也升级了.升到了3.X. 官网:http://www.jboss.org/resteasy 集成使用也非常简单(相比SOAP而言) 第一步:下载jar包 resteasy是托管 ...
- 定制Qt帮助系统
楼主 版权声明 该文章原创于Qter开源社区(www.qter.org),作者yafeilinux,转载请注明出处! 导语 一个完善的应用程序应该提供尽可能丰富的帮助信息.在Qt ...
- samba服务器加入域控主机所需要修改的配置文件
samba服务器加入域控主机,成为域成员,当用户访问samba服务器上的共享文件时,直接到域控主机上进行认证.samba服务器上不需要像先前一样创建系统用户,创建samba用户及密码. 1.安装环境( ...
- 《JavaScript 闯关记》之单体内置对象
ECMA-262 对内置对象的定义是「由 JavaScript 实现提供的.不依赖于宿主环境的对象,这些对象在 JavaScript 程序执行之前就已经存在了」.意思就是说,开发人员不必显式地实例化内 ...
- webservice和.net remoting浅谈
服务器端向客户端发送一个进程编号,一个程序域编号,以确定对象的位置. webservice和.net remoting都是用来通信的框架,它们最大的优点是可以像调用本地对象一样调用远程对象,比如: ...