Isomorphic Strings leetcode
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.
题目很简单,也很容易想到方法,就是记录遍历s的每一个字母,并且记录s[i]到t[i]的映射,当发现与已有的映射不同时,说明无法同构,直接return false。但是这样只能保证从s到t的映射,不能保证从t到s的映射,所以交换s与t的位置再重来一遍上述的遍历就OK了。
举一个例子为什么需要两次映射
如果是 "ega"和"add" 那么g和a都会映射到d,如何要保证一一对应,需要双向映射关系
bool check(string s, string t)
{
unordered_map<char, char> map;
for (int i = ; i < s.length(); ++i)
{
char c1 = s[i];
char c2 = t[i];
if (map.find(c1) == map.end())
map[c1] = c2;
else if (map[c1] != c2)
return false;
}
return true;
} bool isIsomorphic(string s, string t) {
return check(s, t) && check(t, s);
}
Isomorphic Strings leetcode的更多相关文章
- 205. Isomorphic Strings - LeetCode
Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...
- [LeetCode] Isomorphic Strings
Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...
- leetcode:Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- 【刷题-LeetCode】205. Isomorphic Strings
Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...
- LeetCode_205. Isomorphic Strings
205. Isomorphic Strings Easy Given two strings s and t, determine if they are isomorphic. Two string ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces 985 F - Isomorphic Strings
F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代 ...
随机推荐
- Eclipse 发布网站到linux的tomcat
1. 安装Eclipse tomcat插件 2. 打包程序 需要把程序打成war包,右键工程,如下操作: 3. 上传到linux 3.1 上传到tomcat目录下 tomcat/webapps/XXX ...
- oracle11g手工建库
1.设置环境变量 [oracle@HE3~]$ vi .bash_profile exportPATH exportEDITOR=vi exportORACLE_SID=orcl exportORAC ...
- LINQ 的查询_联表、分组、排序
1.查询 var v = from s in db.Set<ScoreInfo>().ToList()group s by s.subject into scoreselect new{ ...
- java Runtime类
public class Test { public static void main(String[] args) throws UnsupportedEncodingException { Run ...
- 故障排查实战案例——某电器ERP系统日志暴增
前言 本篇文章写在新春佳节前夕,也是给IT运维朋友一个警醒,在春节长假前请妥善体检自己的系统安心过个年. 千里之堤毁于蚁穴,一条看似简单的语句就能拖垮整个系统,您的SQL Server很久没体检了吧? ...
- Rabbitmq无法监听后续消息
现象: 消息队列在处理完一条消息后,无法继续监听后续消息. 首先,系统启动时要启动接收方法如下: protected void Application_Start() { RouteTable.Rou ...
- MEAN教程2-Nodejs安装
安装Node.js稳定版本最简单的办法也是使用二进制文件,Node.js官方网站上提供了下载地址,可用于Linux.Mac OS X和Windows系统.同样要注意下载与目标操作系统架构一致的文件. ...
- XmlHepler(拿去就能用)
[系列目录](./XmlCatalog.html) 前言 本篇是这个系列的最后一篇.也是本人第一次写系列文章,虽然系列中大部分内容都是参考网络,但是自己都有敲代码验证.自己再编写过程中也学习到了很多知 ...
- github在windows下的安装和基本使用
1.在win下安装github时花费的时间是非常长的,有时还会出现因各种原因安装不成功.离线包本地安装方便的解决了此问题.点击http://pan.baidu.com/s/1boGrNLP可下载,解压 ...
- 游戏UI框架设计(一) : 架构设计理论篇
游戏UI框架设计(一) ---架构设计理论篇 前几天(2017年2月)看到一篇文章,国内王健林.马云等大咖们看好的未来十大最有"钱途"产业中,排名第一的就是"泛娱乐&qu ...