当前位置:网站首页>力扣每日一题-第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;
}
};
边栏推荐
- Sleuth+Zipkin 来进行分布式服务链路的追踪
- STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
- Producer consumer model of concurrent model
- Matlab learning -- structured programs and user-defined functions
- Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
- Shortcut key for adjusting terminal size in ROS
- Idea configuration web container and war packaging
- Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind
- Rdkit: introduce smiles code, smart code and Morgan fingerprint (ECFP)
- Learn exkmp again (exkmp template)
猜你喜欢

Bingbing learning notes: operator overloading -- implementation of date class

【科技1】

Self study notes on Apache file management -- mapping folders and configuring Apache virtual machines based on single IP and multi domain names

makefile详解

Photo scale correction tool: DxO viewpoint 3 direct mount version

How to solve the time zone problem in MySQL timestamp

for_ Example of each usage

Asynchronous callback future mode of concurrent mode

Pp-yoloe details

Singleton mode (hungry and lazy)
随机推荐
Web uploader cannot upload multiple files
AI platform, AI midrange architecture
Matlab learning -- structured programs and user-defined functions
Simple code implementation of decision tree
July 28, 2022 Gu Yujia's study notes
(2022杭电多校三)1002-Boss Rush(状压DP+二分)
Configure vscade to realize ROS writing
Learn exkmp again (exkmp template)
Server operation management system
Sleuth+Zipkin 来进行分布式服务链路的追踪
Anaconda offline installation environment
MYCAT read / write separation configuration
机器学习【Numpy】
Regular expression bypasses WAF
Sleuth+zipkin to track distributed service links
How to realize shortcut keys for interface scaling by vscade
GJB常见混淆概念
(nowcoder22529C)dinner(容斥原理+排列组合)
Introduction and advanced level of MySQL (12)
ShardingSphere之水平分表实战(三)