Question

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.

Solution 1

Use hashmap, remember to check both key and value. Time complexity O(n^2), space cost O(n).

hashMap.containsValue costs O(n)

 public class Solution {
public boolean isIsomorphic(String s, String t) {
if (s == null || t == null)
return false;
if (s.length() != t.length())
return false;
if(s.length()==0 && t.length()==0)
return true;
int length = s.length();
Map<Character, Character> map = new HashMap<Character, Character>();
for (int i = 0; i < length; i++) {
char tmp1 = s.charAt(i);
char tmp2 = t.charAt(i);
if (map.containsKey(tmp1)) {
if (map.get(tmp1) != tmp2)
return false;
} else if (map.containsValue(tmp2)) {
return false;
} else {
map.put(tmp1, tmp2);
}
}
return true;
}
}

Solution 2

Use extra space to reduce time complexity.

 public class Solution {
public boolean isIsomorphic(String s, String t) {
if (s == null || t == null)
return false;
if (s.length() != t.length())
return false;
if(s.length()==0 && t.length()==0)
return true;
int length = s.length();
Map<Character, Character> map = new HashMap<Character, Character>();
Set<Character> counts = new HashSet<Character>();
for (int i = 0; i < length; i++) {
char tmp1 = s.charAt(i);
char tmp2 = t.charAt(i);
if (map.containsKey(tmp1)) {
if (map.get(tmp1) != tmp2)
return false;
} else if (counts.contains(tmp2)) {
return false;
} else {
map.put(tmp1, tmp2);
counts.add(tmp2);
}
}
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 ...

随机推荐

  1. 关于链表的一些重要操作(Important operations on a Linked List)

    上篇博文中讨论了链表的一些基本操作: 链表的基本操作(Basic Operations on a Linked List) 然而,为创建一个多功能的链表,在深度学习之前我们还需要了解更多的链表操作. ...

  2. HDOJ-1016 Prime Ring Problem(DFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入n,代表有一个包含n个节点的环,在环中的节点中填入1,2...n-1,n,要求填入的数与左边的数 ...

  3. 如何搞定前端资源服务跨域问题之nginx篇

    问题描述 1.首先让我们先看一张图 2.从图中,我们可以很清楚的看到当http请求的站点访问https的资源的时候会报出“Cross-Origin”跨域的问题.为什么会出现这样的错误,这是因为涉及到“ ...

  4. 装Oracle12C时遇到没有权限访问临时位置的解决方法

    今天在装oracle12c是遇到了一个很奇怪的问题,显示是没有权限访问临时位置,可是我明明是用管理员的账号登陆的啊,最后在包姐的帮助下解决了,知其然,而我却不知其所以然.但还是把方法写下,希望能帮到一 ...

  5. asp.net UpdatePanel 不能局部刷新问题汇总

    1.web.config 配置问题.   关于web.config的配置方面网上有很多资料参考,按照其方法做即可实现. 2.网站 Framework 版本变化造成不能局部刷新问题    版本更新时,会 ...

  6. [转]CENTOS6 VNCSERVER安装

    标签:vncservercentos6.0 ssh隧道 vncviewer centos 休闲 职场 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律 ...

  7. ios学习笔记-数据持久化

    沙盒 沙盒是一种数据安全策略,只允许自己的应用访问目录.可以使用NSHomeDirectory()获取. ios沙盒下有三个子目录: 1.Documents目录:用于存储比较大的文件活着需要频发女更新 ...

  8. chisel中pviews命令无法使用

    chisel是用Python写的LLDB调试器插件,用来调试iOS应用非常方便,相关下载安装链接如下:https://github.com/facebook/chisel.本人安装之后,在xcode里 ...

  9. linux下python多版本共存

    为何要安装python,linux下不是已经集成了python吗? 大多数linux系统都集成了python,但是他们的版本太低了.不能满足我们的需求,尤其是好多系统居然仍停留在 python2.6. ...

  10. Hibernate中HQL的日期差值计算,可计算相差多少秒

    最近有个业务需求就是计算订单创建时间离现在超过 4 小时的订单都查找出来! 那么就需要用到日期函数了. 网上找了一下总共的日期函数有一下几个: CURRENT_DATE() 返回数据库当前日期 时间函 ...