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

Example 1:

Input: s = "egg", t = "add"
Output: true

Example 2:

Input: s = "foo", t = "bar"
Output: false

Example 3:

Input: s = "paper", t = "title"
Output: true

确保s->t和t->s都是单射(一一映射),用两个map分别表示正向和反向的映射关系

class Solution {
public:
bool isIsomorphic(string s, string t) {
if(s.size() != t.size())return false;
unordered_map<char, char>mp1, mp2;
for(int i = 0; i < s.size(); ++i){
if(mp1[t[i]] && mp1[t[i]] != s[i])return false;
if(mp2[s[i]] && mp2[s[i]] != t[i])return false;
mp1[t[i]] = s[i];
mp2[s[i]] = t[i];
}
return true;
}
};

【刷题-LeetCode】205. Isomorphic Strings的更多相关文章

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

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

  2. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

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

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

  4. LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

    翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...

  5. Java for LeetCode 205 Isomorphic Strings

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

  6. (easy)LeetCode 205.Isomorphic Strings (*)

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

  7. Java [Leetcode 205]Isomorphic Strings

    题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  8. [LeetCode] 205. Isomorphic Strings 解题思路 - Java

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

  9. leetcode 205. Isomorphic Strings(哈希表)

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

  10. Leetcode 205 Isomorphic Strings 字符串处理

    判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...

随机推荐

  1. Python第三周 数据类型:集合set、文件的读写、追加操作。

    集合 知识点:集合是无序的 格式:{1,2,3,"str_test"} set_1 = set(list1)#将列表转换为集合 集合关系测试: 集合的逻辑判断.取交集.并集.差集. ...

  2. 通过go调用bat解决乱码核心代码

    背景:通过go写的程序调用dos命令(如dir c:),返回值往往有中文,默认会出现乱码. 解决:转码. 代码:GO package shellLib import ( "golang.or ...

  3. Spring支持的常用数据库传播属性和事务隔离级别

    一.事务的传播属性 1.propagation:用来设置事务的传播行为,一个方法运行在了一个开启了事务的方法中时,当前方法是使用原来的事务还是开启一个新的事务. (1)propagation.REQU ...

  4. Android 控件使用教程(一)—— ListView 展示图片

    起因 最近在看一些开源项目时,经常看到了RecyclerView,这是安卓5.0推出的一个新的控件,可以代替传统的ListView,已经这么久了还没有用过,所以决定试一试.另外在做这个的工程中看到了另 ...

  5. 【LeetCode】1004. Max Consecutive Ones III 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法/双指针 日期 题目地址:https://le ...

  6. 【LeetCode】203. Remove Linked List Elements 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 递归 日期 题目地址:https://lee ...

  7. Chapter 14 G-estimation of Structural Nested Models

    目录 14.1 The causal question revisited 14.2 Exchangeability revisited 14.3 Structural nested mean mod ...

  8. 使用pypy3加速python运行

    从这里下载对应OS版本的安装包 解压: tar xf pypy-x.y.z.tar.bz2 然后通过./pypy-x.y.z/bin/pypy可以直接进入console 可以使用pip安装包: ./p ...

  9. <数据结构>图的最短路径问题

    目录 最短路径问题 Dijstra算法:中介点优化 基本步骤 伪代码 在实现过程中的关键问题 代码实现 邻接矩阵版 邻接表版 时间复杂度:O(VlogV+E) 算法存在的问题:存在负权边时会失效 Be ...

  10. Spring进一步学习

    目录 1.beans.xml编写 2.别名 (1)alias (2)name 3.依赖注入(DI) (1)数组注入 (2)List注入 (3)Map注入 (4)set注入 (5)Null注入 (6)p ...