当前位置:网站首页>205. 同构字符串
205. 同构字符串
2022-07-01 03:23:00 【Sun_Sky_Sea】
205. 同构字符串
原始题目链接:https://leetcode.cn/problems/isomorphic-strings/
给定两个字符串 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 字符组成
解题思路:
使用两个哈希表,key是当前字符串的字符,value是对方字符串的字符。
代码实现:
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
d1, d2 = {
}, {
}
for i in range(len(s)):
if ((s[i] in d1 and d1[s[i]] != t[i]) or (t[i] in d2 and d2[t[i]] != s[i])):
return False
d1[s[i]] = t[i]
d2[t[i]] = s[i]
return True
边栏推荐
- C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
- Unexpected token o in JSON at position 1 ,JSON解析问题
- 【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)
- 排序链表(归并排序)
- ECMAScript 6.0
- Edlines: a real time line segment detector with a false detection control
- Binary tree god level traversal: Morris traversal
- Bilinear upsampling and f.upsample in pytorch_ bilinear
- [small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
- 数据库中COMMENT关键字的使用
猜你喜欢
随机推荐
LeetCode 128最长连续序列(哈希set)
ES6解构语法详解
Detailed explanation of ES6 deconstruction grammar
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
C语言的sem_t变量类型
小程序容器技术与物联网IoT的结合点
Unexpected token o in JSON at position 1 ,JSON解析问题
Research on target recognition and tracking based on 3D laser point cloud
Feature Pyramid Networks for Object Detection论文理解
ECMAScript 6.0
Promise中finally的用法
Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
pytorch训练深度学习网络设置cuda指定的GPU可见
整合阿里云短信的问题:无法从静态上下文中引用非静态方法
Jeecgboot output log, how to use @slf4j
Golang multi graph generation gif
ECMAScript 6.0
torch. histc
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载









