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的更多相关文章

  1. 205. Isomorphic Strings - LeetCode

    Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...

  2. [LeetCode] Isomorphic Strings

    Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...

  3. leetcode:Isomorphic Strings

    Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...

  4. [leetcode]205. Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  5. LeetCode 205. 同构字符串(Isomorphic Strings)

    205. 同构字符串 205. Isomorphic Strings

  6. 【刷题-LeetCode】205. Isomorphic Strings

    Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...

  7. LeetCode_205. Isomorphic Strings

    205. Isomorphic Strings Easy Given two strings s and t, determine if they are isomorphic. Two string ...

  8. 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 ...

  9. Codeforces 985 F - Isomorphic Strings

    F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代 ...

随机推荐

  1. Android的文件存储

    //文件的写入 String content1 = edt_file.getText().toString(); //用于文件的写操作 FileOutputStream fos=null; //缓冲输 ...

  2. Express4.x安装

    1.首先肯定是要安装Node.JS npm install -g expressnpm install -g express-generator 运行express -V输出 4.9.0 2.创建一个 ...

  3. 支付宝 Android 版使用的开源组件

    支付宝 Android 版使用的开源组件 前言: 花了点时间整理了 支付宝 Android 客户端使用的开源组件,给需要的同学.在你不知道用什么开源框架的时候可以作下参考,毕竟支付宝是阿里的重量级产品 ...

  4. KVO,看我就够了!

    概述 KVO全称Key-Value-Observing,也叫键值监听,是一种观察者设计模式.提供了一种机制,当指定的对象的属性被修改后,对象就会收到一个通知.也就是说每次指定的被观察的对象的属性被修改 ...

  5. 【长 PI】

    /* 长 PI 说明: 圆周率后的小数位数是无止境的,如何使用电脑来计算这无止境的小数是一些数学家与程式设计师所感兴趣的,在这边介绍一个公式配合 大 数运算,可以计算指定位数的圆周率. 解法 : 首先 ...

  6. 三分钟解读springmvc依赖

    长期以来都在写SSM框架的项目,却未能深入理解框架的搭建原理,而只是浅薄的理解前辈的架构,然后不断套用,项目做过几个,但框架的内涵却没有把握.小编打算今天从SpringMVC的依赖分析做起,一步步进行 ...

  7. 笑谈ArcToolbox (3) ArcToolbox的一亩三分地

    笑谈ArcToolbox (3) ArcToolbox的一亩三分地 by 李远祥 每个人都会有一些鲜为人知的小秘密,都有着不允许别人染指的一亩三分地.软件是人编写的,当然也会给它留有自己所拥有的一亩三 ...

  8. php基础知识掌握——四种界定符

    闲来无事,研究了与java.c#完全不同的领域php,php即(外文名:PHP: Hypertext Preprocessor,中文名:"超文本预处理器"),对于学习php的初学者 ...

  9. TypeScript-01-变量、基本类型和运算符

    基本类型 基本类型有boolean.number.string.array.void.所有类型在TypeScript中,都是一个唯一的顶层的Any Type 类型的自类型.any关键字代表这种类型. ...

  10. git用法-打补丁

    1. git cherry-pick 作用:从一个branch上选择一个commit,添加该commit到另一个branch上. 1. 切换到你想添加commit的分支上. git checkout ...