当前位置:网站首页>【LeetCode-205】同构字符串
【LeetCode-205】同构字符串
2022-08-11 05:30:00 【Ring*】
7.4 同构字符串【205】
7.4.1 题目描述
给定两个字符串 s 和 t ,判断它们是否是同构的。
如果 s 中的字符可以按某种映射关系替换得到 t ,那么这两个字符串是同构的。
每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。
示例 1:
输入:s = "egg", t = "add"
输出:true
示例 2:
输入:s = "foo", t = "bar"
输出:false
示例 3:
输入:s = "paper", t = "title"
输出:true
提示:
- 1 <= s.length <= 5 * 104
- t.length == s.length
- s 和 t 由任意有效的 ASCII 字符组成
7.4.2 方法一:哈希表
此题是「290. 单词规律」的简化版,需要我们判断 s 和 t 每个位置上的字符是否都一一对应,即 s 的任意一个字符被 t 中唯一的字符对应,同时 t 的任意一个字符被 s 中唯一的字符对应。这也被称为「双射」的关系。
以示例 2 为例,t 中的字符 a 和 r 虽然有唯一的映射 o,但对于 s 中的字符 o 来说其存在两个映射 {a,r},故不满足条件。
因此,我们维护两张哈希表,第一张哈希表 s2t \textit{s2t} s2t 以 s 中字符为键,映射至 t 的字符为值,第二张哈希表 t2s \textit{t2s} t2s 以 t 中字符为键,映射至 s 的字符为值。从左至右遍历两个字符串的字符,不断更新两张哈希表,如果出现冲突(即当前下标 index 对应的字符 s[index] 已经存在映射且不为 t[index] 或当前下标 index 对应的字符 t[index] 已经存在映射且不为 s[index])时说明两个字符串无法构成同构,返回 false。
如果遍历结束没有出现冲突,则表明两个字符串是同构的,返回 true 即可。
class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character, Character> s2t = new HashMap<Character, Character>();
Map<Character, Character> t2s = new HashMap<Character, Character>();
int len = s.length();
for (int i = 0; i < len; ++i) {
char x = s.charAt(i), y = t.charAt(i);
if ((s2t.containsKey(x) && s2t.get(x) != y) || (t2s.containsKey(y) && t2s.get(y) != x)) {
return false;
}
s2t.put(x, y);
t2s.put(y, x);
}
return true;
}
}
复杂度分析
- 时间复杂度:O(n),其中 n 为字符串的长度。我们只需同时遍历一遍字符串 s 和 t 即可。
- 空间复杂度:O(∣Σ∣),其中 Σ 是字符串的字符集。哈希表存储字符的空间取决于字符串的字符集大小,最坏情况下每个字符均不相同,需要 O(∣Σ∣) 的空间。
7.4.3 my answer—对比映射数组
思路
// 以例3为例
s = "paper" ====> sNum = [0,1,0,3,4]
t = "title" ====> tNum = [0,1,0,3,4]
// 即将两个字符串分别映射到数组,第一次出现的字符,将其值置为数组下标,第二次重复出现则置为前面的值,故而需要哈希map辅助
// 比较两个数组是否一样即可判断是不是同构字符串
class Solution {
public boolean isIsomorphic(String s, String t) {
int[] sNum = new int[s.length()];
int[] tNum = new int[t.length()];
Map<Character,Integer> sMap = new HashMap<>();
Map<Character,Integer> tMap = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
Character ch = s.charAt(i);
if(sMap.containsKey(ch)){
sNum[i] = sMap.get(ch);
}else {
sNum[i] = i;
}
sMap.put(ch,sMap.getOrDefault(ch,i));
}
for (int j = 0; j < t.length(); j++) {
Character ch = t.charAt(j);
if(tMap.containsKey(ch)){
tNum[j] = tMap.get(ch);
}else {
tNum[j] = j;
}
tMap.put(ch,tMap.getOrDefault(ch,j));
}
return Arrays.equals(sNum,tNum);
}
}
边栏推荐
- Use c language to implement tic-tac-toe chess (with source code, you can run it directly)
- Js method commonly used objects and attributes
- Day 87
- 2022DASCTF X SU 三月春季挑战赛 checkin ROPgadget进阶使用
- JS小技巧,让你编码效率杠杠的,快乐摸鱼
- Regular expression replacement for batch quick modification code
- Promise.race learning (judging the fastest execution of multiple promise objects)
- OpenMLDB Pulsar Connector: Efficiently connect real-time data to feature engineering
- JS advanced web page special effects (pink teacher notes)
- Day 83
猜你喜欢
智能风控中台设计与落地
127.0.0.1 已拒绝连接
OpenMLDB v0.5.0 released | Performance, cost, flexibility reach new heights
heap2 (tcache attack,house of orange)
Wonderful linkage | OpenMLDB Pulsar Connector principle and practical operation
OpenMLDB:线上线下一致的生产级特征计算平台
Intelligent risk control China design and fall to the ground
Use the adb command to manage applications
[Meetup]OpenMLDBxDolphinScheduler 链接特征工程与调度环节,打造端到端MLOps工作流
Day 86
随机推荐
Tinker's self-introduction
js学习进阶BOM部分(pink老师笔记)
127.0.0.1 已拒绝连接
微信小程序云开发项目wx-store代码详解
Day 87
Asis2016 books null off by one
The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~
2021-09-11 C language variables and memory allocation
JS事件循环机制
Day 86
轻松理解进程与线程
详解程序执行过程
精彩联动 | OpenMLDB Pulsar Connector原理和实操
Day 81
【无标题】
Day 73
[Meetup]OpenMLDBxDolphinScheduler 链接特征工程与调度环节,打造端到端MLOps工作流
父子节点数据格式不一致的树状列表实现
Vscode remote connection server terminal zsh+Oh-my-zsh + Powerlevel10 + Autosuggestions + Autojump + Syntax-highlighting
第一章 Verilog语言和Vivado初步使用