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. (转)前端构建工具gulp入门教程

    前端构建工具gulp入门教程 老婆婆 1.8k 2013年12月30日 发布 推荐 10 推荐 收藏 83 收藏,20k 浏览 本文假设你之前没有用过任何任务脚本(task runner)和命令行工具 ...

  2. 自己编写基于MVC的轻量级PHP框架

    做WEB开发已有三年,每次都写重复的东西, 因此,想自己写一下框架,以后开发方便.本人之前asp.NET一年开发,jsp半年,可是后来因为工作的原故换成PHP.其实很不喜欢PHP的语法.还有PHP的函 ...

  3. 62.在cdc文件上某些例化模块看不到的原因

    比如在顶层文件中,例化了几个模块,综合后打开cdc文件,会在structure/net中少几个例化模块,即看不到,但在顶层文件中还是存在的,只是ISE软件综合的问题而已,原因是在顶层或子模块中,有些应 ...

  4. 28.USB的传输类型

    USB上必须将数据组织成 事务 才能够进行传输.事务常有两个或三个包.令牌包用于启动一个事务,由主机发送:数据包传送数据,方向由令牌包确定:握手包常是数据接收方发送的,用于表示接收数据的状态.USB协 ...

  5. C++实现禁忌搜索解决TSP问题

    C++实现禁忌搜索解决TSP问题 使用的搜索方法是Tabu Search(禁忌搜索) 程序设计 1) 文件读入坐标点计算距离矩阵/读入距离矩阵 for(int i = 0; i < CityNu ...

  6. PB串口编程资料(转)

    ----MSCOMM32参数基本介绍2008年03月29日 星期六 上午 11:37MSComm 控件通过串行端口传输和接收数据,为应用程序提供串行通讯功能.MSComm控件在串口编程时非常方便,程序 ...

  7. UIView 添加子视图的常用方法

    1.  - (void)addSubview:(UIView *)view 这是最常用的方法有两个注意点 参数view可以是nil,运行不会报错,当然,父视图的subViews也不会增加. 此方法增加 ...

  8. 如何使用css和jquery控制文章标题字数?

    如何使用css控制文章标题字数? 最佳答案 控制文章标题字数,不是动态网页的专利,如果静态页面使用CSS样式,也可以实现相同的效果! 看这个例子,你们可以复制到记事本保存为HTML文件看效果! < ...

  9. linux打包压缩命令汇总

    tar命令 [root@linux ~]# tar [-cxtzjvfpPN] 文件与目录 ....参数:-c :建立一个压缩文件的参数指令(create 的意思):-x :解开一个压缩文件的参数指令 ...

  10. SQL SERVER数据库修改是否区分大小写

    昨天去客户,发现程序无法应用,跟踪错误提示,提示的大致意思是“数据库表名和数据库字段名不存在”.查询后发现是SQL Server数据库设置了区分大小写的缘故(一般安装时,Oracle的正确安装下是默认 ...