当前位置:网站首页>力扣每日一题-第44天-205. 同构字符串
力扣每日一题-第44天-205. 同构字符串
2022-07-29 03:32:00 【重邮研究森】
2022.7.28今天你刷题了吗?
题目:
给定两个字符串 s 和 t ,判断它们是否是同构的。
如果 s 中的字符可以按某种映射关系替换得到 t ,那么这两个字符串是同构的。
每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。
分析:
给定两个字符串,判断是不是同构,说白了就是映射,具体举例如下
s1=“abcdd” s2=“qwerr”
那么相当于 a=b b=w c=e d=r ,因此满足
s1=“abcddr” s2=“qwerrq”
那么相当于 a=b b=w c=e d=r ,但最后一位r=q和前面不满足,则不满足
思路:利用两个哈希表来存放两个字符串,在存放的过程中需要先判断哈希表中是否已经存在这个元素,如果存在,那么判断当前这个元素对应的值是否和之前保留的值一样
解析:
1.哈希表
class Solution {
public:
bool isIsomorphic(string s, string t) {
unordered_map<char, char>s1;
unordered_map<char, char>t1;
for (int i = 0; i < s.size(); i++)
{
char x = s[i], y = t[i];
//存在该元素,且该元素当前映射值和之前映射值不一样
if (s1.count(x)&&s1[x]!=y||t1.count(y)&&t1[y]!=x)
{
return false;
}
s1[x] = y;
t1[y] = x;
}
return true;
}
};
边栏推荐
- Anaconda offline installation environment
- (codeforce547)C-Mike and Foam(质因子+容斥原理)
- 军品研制过程-转阶段
- HDU multi School Game 2 1011 DOS card
- Swing V2: towards a larger model with larger capacity and higher resolution
- ROS-Errror:Did you forget to specify generate_ messages(DEPENDENCIES ...)?
- Introduction and advanced MySQL (13)
- 04 | background login: login method based on account and password (Part 1)
- Photo scale correction tool: DxO viewpoint 3 direct mount version
- web-uploader不能多文件上传
猜你喜欢

今晚7:30 | 连界、将门、百度、碧桂园创投四位大佬眼中的AI世界,是继续高深还是回归商业本质?...

VISO fast rendering convolution block
![MOS管 —— 快速复苏应用笔记(贰)[参数与应用]](/img/54/eb040a51304192def8cfb360c7c213.png)
MOS管 —— 快速复苏应用笔记(贰)[参数与应用]

MYCAT read / write separation configuration

年内首个“三连跌” 95号汽油回归“8元时代“

Arm architecture and neural network

暴力递归到动态规划 01 (机器人移动)

Rongyun real-time community solution

The Federal Reserve raised interest rates again, Powell "let go of doves" at 75 basis points, and US stocks reveled

Rdkit: introduce smiles code, smart code and Morgan fingerprint (ECFP)
随机推荐
1.4 nn. Module neural network (II)
3.2 model saving and loading
军品三大基线(功能基线、分配基线、产品基线)及基线包含的文件
How to realize shortcut keys for interface scaling by vscade
机器学习【Numpy】
for_each用法示例
Violence recursion to dynamic programming 01 (robot movement)
Typescript learning (I)
Anti vulnerability · benefit from uncertainty --- management?
Rongyun IM & RTC capabilities on new sites
Shardingsphere's level table practice (III)
Reproduce 20 character short domain name bypass and XSS related knowledge points
three. JS Part 54 how to pass structure array to shader
How to realize multi line annotation in MATLAB
逐步分析类的拆分之案例——五彩斑斓的小球碰撞
During the year, the first "three consecutive falls" of No. 95 gasoline returned to the "8 Yuan era"“
后缀自动机(sam)板子 from jly
Calculation of array serial number of force deduction questions (daily question 7/28)
Sleuth+Zipkin 来进行分布式服务链路的追踪
深入C语言(4)——switch的定义与使用