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.

解题思路:

用两张图保存下两个方向的映射即可,JAVA实现如下:

    public boolean isIsomorphic(String s, String t) {
HashMap<Character, Character> hm = new HashMap<Character, Character>();
HashMap<Character, Character> hm2 = new HashMap<Character, Character>();
for (int i = 0; i < s.length(); i++) {
if (hm.containsKey(s.charAt(i))
&& hm.get(s.charAt(i)) != t.charAt(i))
return false;
hm.put(s.charAt(i), t.charAt(i));
if (hm2.containsKey(t.charAt(i))
&& hm2.get(t.charAt(i)) != s.charAt(i))
return false;
hm2.put(t.charAt(i), s.charAt(i));
}
return true;
}

Java for 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 解题思路 - Java

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

  3. Java [Leetcode 205]Isomorphic Strings

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

  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

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

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

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

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

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

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

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

  9. Leetcode 205 Isomorphic Strings 字符串处理

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

随机推荐

  1. 使用jQuery的Scrollify插件实现鼠标滚轮或者手势滑动到页面下一节点部分

    有时我们需要做一个单页面介绍产品特性,而单页面内容非常多且页面非常长,为了快速定位到产品特性节点,我们使用js侦听用户滚轮事件,当用户触发滚轮滑动或者使用手势触屏滑动时,即可定位到相应的节点.一款jQ ...

  2. 在hexo静态博客中利用d3-cloud来展现标签云

    效果: http://lucyhao.com/tags/ hexo自带的tag cloud的标签展现不太美观,想能够展现出“云”效果的标签.在网上找到了d3-cloud这个项目,github地址:ht ...

  3. sqlserver中将某数据库下的所有表字段名称为小写的改为大写

    declare @name varchar(50), @newname varchar(50),@colname varchar(50) declare abc cursor for select ( ...

  4. ASN.1编码

    来自几年前本人写的一篇博客 http://blog.csdn.net/newyf_cun/article/details/13016069 如下使用libtasn1分析asn1的编码规则. http: ...

  5. CentOS-6.5-NFS部署

                      nfs-server与nfs-client端配置一样   NFS(network file system)网络文件系统:用于在网络上共享存储. 服务端-192.16 ...

  6. ubuntu 15.04 手动安装nginx 1.9.0

    平时工作也用nginx,不过用的时候都是已经配好的,只要简单改改参数就可以了.今天在自己的电脑上安装的时候发现没有想象的那么顺利. 纸上得来终觉浅,绝知此事要躬行. 正题: 1.到nginx下载页面获 ...

  7. 【转载】javaAgent 参数

    -javaagent 这个JVM参数是JDK 5引进的. java -help的帮助里面写道: -javaagent:<jarpath>[=<options>] load Ja ...

  8. Java 的printf(转)

    出处:http://blog.csdn.net/swandragon/article/details/4653600 public class TestPrintf{public static voi ...

  9. RHEL/CentOS 7最小化安装后需做的30件事情

    导读 CentOS是一个工业标准的Linux发行版,是红帽企业版 Linux 的衍生版本.你安装完后马上就可以使用,但是为了更好地使用你的系统,你需要进行一些升级.安装新的软件包.配置特定服务和应用程 ...

  10. Android应用签名

    http://www.cnblogs.com/ghj1976/archive/2011/07/18/2109381.html 为了要签名? 开发Android的人这么多,完全有可能大家都把类名,包名起 ...