当前位置:网站首页>leetcode-同构字符串判断
leetcode-同构字符串判断
2022-08-08 04:25:00 【zjLOVEcyj】
- 同构字符串
给定两个字符串 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 字符组成
let isIsomorphic = function(s, t) {
//长度不同返回false
if (s.length !== t.length) return false
let map1 = {
}//建立映射表
for (let i = 0; i < s.length; i++) {
if (!map1[s[i]]) {
//映射表中没有的话加入进去
map1[s[i]] = t[i]
} else {
//映射表中已有的话判断是否符合之前的映射 不符合直接返回false
if (t[i] !== map1[s[i]]) return false
}
}
//最后判断映射表的value是否全是不同字符,即符合不同字符不能映射到相同字符的要求 不符合的话返回false
if (Array.from(new Set(Object.values(map1))).length !== Object.values(map1).length) return false
return true
};
console.log(isIsomorphic('ab', 'cc'));

边栏推荐
- 项目分析(嵌入式产品Web化)
- Redis persistence mechanism, master-slave, sentry, cluster parsing cluster solution
- 内修昇思MindSpore AI框架,外重行业汇聚,华为大模型的不平凡之路
- 32. 你知道Redis的字符串是怎么实现的吗?
- leetcode: 455. 分发饼干
- Knowledge of DisplayPort-DP interface
- 全网唯一OpenCyphal/UAVCAN教程(11)用candump和gawk工具写一个Cyphal协议解析小工具
- C# "Original CSharp" 4th Chapter It is common for people to change in time, but it is difficult to know the succession of humanities
- 实现Express中间件原理
- NetCore使用Dapper查询数据
猜你喜欢

Machine Learning Notes: Learning Rate Warmup

The storage principle of NorFlash

【模板引擎】velocity

egg-Alibaba Cloud SMS Configuration

ToDesk企业版上新 | 十大新功能,让企业远控更安全、更便捷、更流畅

leetcode 112.路经总和 递归

初出茅庐的小李第115篇博客项目笔记之国产GD32F103RCT6基础工程创建

Week 4 Step by step building multi-layer neural network and application (1 & 2)

【多任务模型】《Multi-Faceted Hierarchical Multi-Task Learning for a Large Number of Tasks with Multi-dimens

fail-fast 和 fail-safe 快速学习
随机推荐
【Template Engine】velocity
08 获取器 withAttr、多连缀、whereRaw、事务、数据集《ThinkPHP6 入门到电商实战》
LeetCode_485_最大连续1的个数
Awk syntax-03-awk expressions (if statements, while loops, for loops), execute shell commands in awk
6G-Oriented Communication Perception Integrated Architecture and Key Technologies
vulnhub-DC-3 drone penetration record
fail-fast 和 fail-safe 快速学习
Machine Learning Notes: Learning Rate Warmup
语音鉴定软件
Inside outside l think MindSpore AI framework, heavy industry gathering, huawei big extraordinary path of the model
Shell 脚本 — 多行注释、开启子/不开启子进程执行、转义带颜色输出、读取键盘输入、输入输出重定向、单双引号、命令替换、读取变量、系统变量、正则过滤、算术运算、一行多条命令、字符串比较
The 5 most mainstream project time management systems in China
2022/08/06 Study Notes (day24) Collection
初出茅庐的小李第115篇博客项目笔记之国产GD32F103RCT6基础工程创建
MySQL4(多表查询)
package package
Qt 事件学习参考的一些优秀博客推荐
torch.view()函数用法
使用ffmpeg解码音频sdl(push)播放
The effect of base 0 or base 1 on the number of image iterations