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到T 和T到S都检查一遍。

 public class Solution {
public boolean isIsomorphic(String s, String t) {
if (s.length() != t.length()) return false; Map<Character, Character> map = new HashMap<>(); for (int i = ; i < s.length(); i++) {
char chKey = s.charAt(i);
char chValue = t.charAt(i); if(map.containsKey(chKey)) {
char value = map.get(chKey);
if (value != chValue) {
return false;
}
} else {
map.put(chKey, chValue);
}
} map.clear(); for (int i = ; i < s.length(); i++) {
char chKey = t.charAt(i);
char chValue = s.charAt(i); if(map.containsKey(chKey)) {
char value = map.get(chKey);
if (value != chValue) {
return false;
}
} else {
map.put(chKey, chValue);
}
} return true;
}
}

Isomorphic Strings的更多相关文章

  1. [LeetCode] Isomorphic Strings

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

  2. leetcode:Isomorphic Strings

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

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

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

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

  5. Codeforces 985 F - Isomorphic Strings

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

  6. Educational Codeforces Round 44 (Rated for Div. 2) F - Isomorphic Strings

    F - Isomorphic Strings 题目大意:给你一个长度为n 由小写字母组成的字符串,有m个询问, 每个询问给你两个区间, 问你xi,yi能不能形成映射关系. 思路:这个题意好难懂啊... ...

  7. CodeForces985F -- Isomorphic Strings

    F. Isomorphic Strings time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

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

    205. 同构字符串 205. Isomorphic Strings

  9. LeetCode_205. Isomorphic Strings

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

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

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

随机推荐

  1. docker管理shipyard中文版v3.0.2更新

    shipyard中文版v3.0.2更新  https://console.dockerclub.net/ https://dockerclub.net/docs/intro/getting_start ...

  2. servlet的九大内置对象

    隐式对象 说明 request 转译后对应HttpServletRequest/ServletRequest对象 response 转译后对应HttpServletRespons/ServletRes ...

  3. CMD窗口如何调整大小 / 颜色

    Windows默认的命令行工具CMD暴丑无比..很多人都会因为这个原因去寻找漂亮的命令行工具.. 但是很多所谓的命令行工具并不能完美的支持到CMD.. 譬如 PowerCMD 或 Cmder 之流.. ...

  4. Yii2.0高级框架数据库增删改查的一些操作(转)

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...

  5. ASP.NET 生命周期

    学习资料:http://www.cnblogs.com/OceanEyes/archive/2012/08/13/2635657.html

  6. plsql常用函数汇总

    在SQLPLUS下,实现中-英字符集转换alter session set nls_language='AMERICAN';alter session set nls_language='SIMPLI ...

  7. c# 多线程 --Mutex(互斥锁)

    互斥锁(Mutex) 互斥锁是一个互斥的同步对象,意味着同一时间有且仅有一个线程可以获取它. 互斥锁可适用于一个共享资源每次只能被一个线程访问的情况 函数: //创建一个处于未获取状态的互斥锁 Pub ...

  8. wtforms 使用

    wtforms是一个表单模板库, 下面以修改密码表单为例简单说明其用法. 我们可以用python代码定义form的基本元素, 比如用户名/邮箱, 并给定各个元素的validation条件. 然后在re ...

  9. VCF (Variant Call Format)格式详解

    文章来源:http://www.cnblogs.com/emanlee/p/4562064.html VCF文件示例(VCFv4.2) ##fileformat=VCFv4.2 ##fileDate= ...

  10. linux之vim编辑神器

    设置编辑器:export EDITOR=vim 1.ctrl+x ctrl+e  创建vim临时文件 2.ctrl+[ 退回到命令模式 3.命令模式下ZZ,保存退出 4.大写I行首插入,大写A行尾插入 ...