leetcode 205
2024-09-17 05:03:06
205. Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given "egg"
, "add"
, return true.
Given "foo"
, "bar"
, return false.
Given "paper"
, "title"
, return true.
Note:
You may assume both s and t have the same length.
设定两个字符串长度相等,所谓的“同构”,就是字符串 s 中的字符可以一对一的映射到字符串 t 中的字符。不能一对多,也不能多对一。
利用两个map实现。
代码如下:
class Solution {
public:
bool isIsomorphic(string s, string t) {
map<char, char> mapA;
map<char, char> mapB;
int n = s.length();
for(int i = ; i < n; i++)
{
char ss = s[i];
char tt = t[i];
map<char, char>::iterator it = mapA.find(ss);
if(it != mapA.end())
{
if(mapA[ss] != tt)
{
return false;
}
}
else
{
map<char, char>::iterator ii = mapB.find(tt);
if(ii != mapB.end() && mapB[tt] != ss)
{
return false;
}
else
{
mapA[ss] = tt;
mapB[tt] = ss;
}
}
}
return true;
}
};
最新文章
- TortoiseSVN 合并操作简明教程
- window配置nginx+php+mysql
- js String对象
- Linux内核设计第七周 ——可执行程序的装载
- 【转】SVN服务器客户端以及环境的搭建和使用
- linux软件的安装,更新与卸载
- linux 打补丁
- Android中ListView分页加载数据
- mysql读写分离
- JavaScript入门之数组:Array类型详解
- 学习总结:工程管理与makefile
- Azkaban-2.5及Plugins的安装配置
- 值类型和引用类型的区别,struct和class的区别
- 谈一谈对MySQL InnoDB的认识及数据库事物处理的隔离级别
- discuz 文件模板edit
- Mapreduce运行过程分析(基于Hadoop2.4)——(二)
- C# Language Specification 5.0 (翻译)第六章 转换
- 【HDU 3590】 PP and QQ (博弈-Anti-SG游戏,SJ定理,树上删边游戏)
- Notes of Daily Scrum Meeting(11.8)
- 1009 Product of Polynomials (25)(25 point(s))