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.

Note:
You may assume both s and t have the same length.

Subscribe to see which companies asked this question

解题
定义HashMap 
public class Solution {
public boolean isIsomorphic(String s, String t) {
if(s==null || t ==null)
return false;
if(s.length()!=s.length())
return false;
if(s==null && t == null)
return true;
HashMap<Character,Character> map = new HashMap<Character,Character>();
for(int i = 0;i< s.length();i++){
char c1 = s.charAt(i);
char c2 = t.charAt(i);
Character c = getKey(map,c2);
if(c !=null && c!=c1)
return false;
else if(map.containsKey(c1)){
if(c2!=map.get(c1))
return false;
}else{
map.put(c1,c2);
}
}
return true;
} public Character getKey(HashMap<Character,Character> map,Character target){
for(Map.Entry<Character,Character> entry:map.entrySet()){
if(entry.getValue().equals(target)){
return entry.getKey();
}
}
return null;
}
}

或者

public class Solution {
public boolean isIsomorphic(String s, String t) {
if(s==null || t ==null)
return false;
if(s.length()!=s.length())
return false;
if(s==null && t == null)
return true;
HashMap<Character,Character> map = new HashMap<Character,Character>();
for(int i = 0;i< s.length();i++){
char c1 = s.charAt(i);
char c2 = t.charAt(i);
Character c = map.get(c1);
// key c1 value c2
if(map.containsKey(c1)){
if(map.get(c1).equals(c2))
continue;
else
return false;
}else{
if(!map.containsValue(c2))
map.put(c1,c2);
else
return false; } }
return true;
} }

这里的字符串都是字母,前256个字符,将其映射到后256个字符

public class Solution {
public boolean isIsomorphic(String s, String t) {
if(s==null || t ==null)
return false;
if(s.length()!=s.length())
return false;
if(s==null && t == null)
return true;
int[] m = new int[512];
for (int i = 0; i < s.length(); i++) {
if (m[s.charAt(i)] != m[t.charAt(i)+256])
return false;
m[s.charAt(i)] = m[t.charAt(i)+256] = i+1;
}
return true;
} }

lintcode :同构字符串的更多相关文章

  1. [LeetCode] 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

    题目: 给定两个字符串 s 和 *t*,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 *t* ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字 ...

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

    205. 同构字符串 205. Isomorphic Strings

  4. Java实现 LeetCode 205 同构字符串

    205. 同构字符串 给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序. ...

  5. [Swift]LeetCode205. 同构字符串 | Isomorphic Strings

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

  6. 【leetcode 简单】 第五十九题 同构字符串

    给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一 ...

  7. LeetCode OJ:Isomorphic Strings(同构字符串)

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

  8. Q205 同构字符串

    给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一 ...

  9. 205 Isomorphic Strings 同构字符串

    给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...

随机推荐

  1. iOS七大手势之(平移、捏合、轻扫、屏幕边缘轻扫)手势识别器方法

    使用手势很简单,分为两步: 创建手势实例.当创建手势时,指定一个回调方法,当手势开始,改变.或结束时,回调方法被调用. 添加到需要识别的View中.每个手势只对应一个View,当屏幕触摸在View的边 ...

  2. 2015年iOS测试现状

    本文由 伯乐在线 - nathanw 翻译,dopcn 校稿.未经许可,禁止转载! 英文出处:www.mokacoding.com.欢迎加入翻译小组. 几周前,我决定将将我在 mokacoding 上 ...

  3. mini6410-JNI-led

    一.编写JNI模块 当安装好NDK编译环境后,会在它的目录下找到sample目录,它里面有一些例子,可以参考这些例子来写我们自已的模块. 1.在/home/android/文件夹下,新建“ledjni ...

  4. Jackson如何使JSON输出变得优雅?

    本篇文章翻译自:How to enable pretty print JSON output (Jackson) 在这篇文章中,我们将教你如何利用Jackson Library在控制台或者JSP页面优 ...

  5. windows多线程编程(一)(转)

    源出处:http://www.cnblogs.com/TenosDoIt/archive/2013/04/15/3022036.html CreateThread:Windows的API函数(SDK函 ...

  6. 使用高德地图SDK获取定位信息

    使用高德地图SDK获取定位信息 第一步: 在高德官网下载SDK,如我这里需要获取定位信息,故仅下载"定位功能" 第二步: 注册成为开发者,并创建应用获取相应的key.其中,在使用A ...

  7. CSS中盒子模型和position(一)

    今天遇到几个css中的重要的知识点,记得这些都是以前看过的:margin.padding.border和position.可是用起来还是有很多的问题,以前自己看过去总是懒得记录,等到用起来了都不知道自 ...

  8. gmail邮箱怎么批量删除邮件

    转载:http://jingyan.baidu.com/article/9f7e7ec056cbcd6f2815543c.html 首先打开gmail邮箱,随便打开一封邮件,找到发件人邮件地址,复制, ...

  9. 两个SVN仓库之间代码的转移

    背景: 1.公司的svn服务器架设在了公司内网环境中,没有公网ip,所以离开了公司环境就无法访问(更新.提交...)svn服务器了: 2.四个开发人员去客户现场开发新的需求,在现场找了一台服务器临时搭 ...

  10. git删除远程分支和本地分支

    问题描述:       当我们集体进行项目时,将自定义分支push到主分支master之后,如何删除远程的自定义分支呢 问题解决:        (1)使用命令git branch -a 查看所有分支 ...