LeetCode 205:同构字符串 Isomorphic Strings
题目:
给定两个字符串 s 和 *t*,判断它们是否是同构的。
如果 s 中的字符可以被替换得到 *t* ,那么这两个字符串是同构的。
所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
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.
示例 1:
输入: s = "egg", t = "add"
输出: true
示例 2:
输入: s = "foo", t = "bar"
输出: false
示例 3:
输入: s = "paper", t = "title"
输出: true
说明:
你可以假设 s 和 *t* 具有相同的长度。
Note:
You may assume both *s* and *t* have the same length.
解题思路:
在示例 3 中输入: s = "paper", t = "title",其中字母映射结果:p <==> t , a <==>i , e <==> l , r <==>e 映射的字母可以一一替换得到对应的单词,这便是同构字符串。
非同构字符串无非两种情况(假设长度相等):
s = 'aa' , t = 'ab',在建立过字母映射a <==> a后,s第二个字母 a 其映射value = a不等于 t 中第二个字母 bs = 'ab' , t = 'aa',在建立过字母映射a <==> b后,t第二个字母 a 其映射key = a不等于s中第二个字母 b
所以这个可以用两个哈希映射验证上述两种情况,也可以用一个映射加判断是否存在于其 values 中。
既然是建立映射字符,那么最先想到的就是哈希映射(map、dict)了。
该题为英文单词字符串同构检测,整个 ASCll 码长度才256,所以这道题也可以用 char[256] 以索引值对应一个字符,其存储值对应一个字符建立映射关系。
还有一个更巧妙的解法,每个字符都与该字符串中第一次出现的索引对比是否相等,判断是否同构。如:
输入:s = 'aa' , t = 'ab'
第一次遍历:
s中第一个字符 a 第一次出现的索引为0,t中第一个字符 a 第一次出现的索引为0,索引相等,继续遍历
第二次遍历:
s中第二个字符 a 第一次出现的索引为0,t中第二个字符 b 第一次出现的索引为1,索引不相等, 返回false
代码:
双哈希映射:
Java:
class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character, Character> s_map = new HashMap<>();
Map<Character, Character> t_map = new HashMap<>();
char[] s_chars = s.toCharArray(), t_chars = t.toCharArray(); // 转成 cahr 型数组
for (int i = 0; i < s_chars.length; i++) {
// 两种不成立的情况
if (s_map.containsKey(s_chars[i]) && s_map.get(s_chars[i]) != t_chars[i]) return false;
if (t_map.containsKey(t_chars[i]) && t_map.get(t_chars[i]) != s_chars[i]) return false;
s_map.put(s_chars[i], t_chars[i]);
t_map.put(t_chars[i], s_chars[i]);
}
return true;
}
}
Python:
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
s_map, t_map = {}, {} # 双字典
for c1, c2 in zip(s, t):
# 两种不成立的情况
if c1 in s_map and s_map[c1] != c2:
return False
if c2 in t_map and t_map[c2] != c1:
return False
s_map[c1] = c2
t_map[c2] = c1
return True
单哈希映射:
Java:
class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character, Character> map = new HashMap<>();// 一个哈希映射
char[] s_chars = s.toCharArray(), t_chars = t.toCharArray();
for (int i = 0; i < s_chars.length; i++) {
if (map.containsKey(s_chars[i]) && map.get(s_chars[i]) != t_chars[i]) return false;
// 判断t中字符是否存在于映射中的 values 内
if (!map.containsKey(s_chars[i]) && map.containsValue(t_chars[i])) return false;
map.put(s_chars[i], t_chars[i]);
}
return true;
}
}
Python:
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
hash_map = {}
for c1, c2 in zip(s, t):
if c1 in hash_map and hash_map[c1] != c2:
return False
# 判断t中字符是否存在于映射中的 values 内
if c1 not in hash_map and c2 in hash_map.values():
return False
hash_map[c1] = c2
return True
字符首次出现的索引对比法:
Java:
class Solution {
public boolean isIsomorphic(String s, String t) {
char[] s_chars = s.toCharArray();
char[] t_chars = t.toCharArray();
for (int i = 0; i < s.length(); i++) {
// 判断该字符首次出现索引值是否相等
if (s.indexOf(s_chars[i]) != t.indexOf(t_chars[i])) {
return false;
}
}
return true;
}
}
Python:
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
for c1, c2 in zip(s, t):
# 判断该字符首次出现索引值是否相等
if s.find(c1) != t.find(c2):
return False
return True
256位字符映射
Java
class Solution {
public boolean isIsomorphic(String s, String t) {
char[] s_chars = s.toCharArray(), t_chars = t.toCharArray();
char[] s_map = new char[256], t_map = new char[256]; //索引与存储值建立映射
for (int i = 0; i < s_chars.length; i++) {
char sc = s_chars[i], tc = t_chars[i];
if (s_map[sc] == 0 && t_map[tc] == 0) {
s_map[sc] = tc;
t_map[tc] = sc;
} else if (s_map[sc] != tc || t_map[tc] != sc) {//索引与元素值的映射是否满足条件
return false;
}
}
return true;
}
}
python中没有字符这一基础数据

欢迎关注微。信。公。众。号:爱写Bug
LeetCode 205:同构字符串 Isomorphic Strings的更多相关文章
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- Java实现 LeetCode 205 同构字符串
205. 同构字符串 给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序. ...
- [Swift]LeetCode205. 同构字符串 | Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- leetcode.字符串.205同构字符串-Java
1. 具体题目 给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不 ...
- LeetCode 859. 亲密字符串(Buddy Strings) 23
859. 亲密字符串 859. Buddy Strings 题目描述 给定两个由小写字母构成的字符串 A 和 B,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true:否则返 ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 205. Isomorphic Strings - LeetCode
Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...
- 【刷题-LeetCode】205. Isomorphic Strings
Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...
随机推荐
- Nginx代理前端代码
Nginx 安装配置 Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/ ...
- V4 Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 2471245.1)
V4 Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 2471245. ...
- 表单生成器(Form Builder)之mongodb表单数据查询——关联查询
这一篇接着记录一下查询相关的操作.想象一下,如果想要在一张表格中展示某些车辆的耗损和营收情况,我们该怎么处理.车辆.耗损.营收各自存储在一张表中,耗损和营收中冗余了车辆信息……我们便想到了关联查询.m ...
- 常用注解解析(因为不太明白@component和@configuration写了)
1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层 2.@service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理 3.@rep ...
- Appium使用总结
目前在使用appium过程中遇到的一些问题及规避方法总结如下: Appium使用总结:1.在熄屏下启动测试,会自动唤醒屏幕2.Appium只针对单个应用测试3.使用unittest框架,该框架中每条用 ...
- November 24th, Week 48th, Sunday, 2019
Once you replace negative thoughts with positive ones, you will start having positive results. 淘汰消极思 ...
- C# 让你解决方案乱七八糟的DLL放入指定文件夹
嗯,大家的解决方案可能会有许多dll,这样不美观,而且也麻烦. 很多小白都不知道如何将这些dll放到如自己程序的bin文件夹下. 本渣今天来试着将dll复制到指定的文件夹下~ 比如我之前做的一个Win ...
- NumPy 会自动检测并利用 GPU 吗?
PyTorch 官网 60 分钟入门教程在介绍什么是 PyTorch 时有一句话:A replacement for NumPy to use the power of GPUs PyTorch 是 ...
- 网易发起“疾风”智造IoT联盟,深化“互联网+先进制造业”发展
7月26日,2019网易云创峰会在杭州拉开序幕,本次峰会以“连接•洞察•进化”为主题,汇聚行业领袖.技术大咖及业界代表,探讨技术演进与行业应用趋势,研商生态合作升级.共赢未来的道路.上午的主论坛中,网 ...
- java超市购物管理系统
一.概述 1.鹏哥前面有写过java项目超市管理系统项目,传送门 2.收到很多朋友私信给我,也很感谢老铁们的反馈和交流,前面这个项目只是对java基础知识和面向对象的思想练习,但是没有涉及到java如 ...