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. 在 App 扩展和主 App 间共享数据

    tags: iOS 8,Swift,App Groups 随着 iOS 8 的发布,苹果为广大开发者很多新的 API,其中最突出显著的就非 App Extension 莫属了.这为开发者们又带来了很多 ...

  2. exception -----> Functions

    /* current_exception */ exception_ptr current_exception() noexcept; 返回指向当前异常(或其副本)的智能指针[具体返回对象本身还是副本 ...

  3. verilog运算符及表达式

    1.运输符 算术运算符(+,-,X,/,%) 赋值运算符(=,<=) 关系运算符(>,<,>=,<=) 逻辑运算符(&&,||,!)//与或非 条件运算符 ...

  4. Ubuntu 12.04 Desktop安装XAMPP

    1/打开终端 在Dash里搜索.打开Dash,在里面搜索“gnome-terminal”,就可以找到终端应用序.快捷键Ctrl+Alt+L也可以,不过如果是虚拟机的话可能会有问题. 如果想以后快捷打开 ...

  5. 一样的Android,不一样的学习

    这几年,Android开始慢慢流行起来,很多项目也开始涉及这部分内容,所以学习Android也就变的很有意义了. 学什么 学习Android应该学什么,很多人有不同的见解.一般程序员可能只是学习And ...

  6. JAVA事务

    一.什么是事务 我们通常会认为事务与数据库有关. 事务是访问数据库的一个操作序列,数据库应用系统通过事务集来完成对数据库的操作.事务的正确执行使得数据库从一种状态转换成另外一种状态. 事务必须服从IS ...

  7. UIWebView swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  8. [shell基础]——算术运算

    shell只支持整数运算.一般可用let.expr.declare.$[]实现. 更精准的运算建议使用Linux下的bc工具——一款高精度计算语言. 1. let是shell内建的整数运算命令 ## ...

  9. DebugViewHierarchy

    DebugViewHierarchy(视图调试)是XCode6新出的一项功能,它可以让开发者在程序运行时,动态的查看当前界面的显示情况,包括视图的层次,控件的大小和位置,而且会以3D效果显示当前视图的 ...

  10. 微软职位内部推荐-Senior Software Engineer-Office Incubation

    微软近期Open的职位: Office China team is looking for experienced engineers to improve consumer experience i ...