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. 集成友盟分享SDK报错

    删除4.2.1版本的reference换成4.3版本运行报错 解决办法:要将4.2.1版本的全部库文件物理删除,不要只删除reference.

  2. EF - 批量插入

    比较一下下面两种方式的区别 1,每Add一次 就savechange() static void Main(string[] args) { //List<User> users= Fin ...

  3. EF6 在原有数据库中使用 CodeFirst 总复习(二、新的需求,简单修改原有表)

    一.为当前实体模型启用数据迁移 基础搭建好了,也就是原有的数据库有了,原有数据库的实体对象也有了,但生成的上下文中并没有标记当前数据库是否已经存在,当前实体是否修改过(以前版本好像有标记的),所以,要 ...

  4. 转Oracle字符集问题总结

    Oracle字符集问题总结 分类: Oracle2006-06-04 13:48 1298人阅读 评论(3) 收藏 举报 oracle数据库sqlcharacter存储insert 作者: vston ...

  5. Android BLE API: GATT Notification not received

    When setting the value to the descriptor instead of putting descriptor.setValue(BluetoothGattDescrip ...

  6. spring字符编码设置

    <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springf ...

  7. C#如何设置Listview的行高-高度

    Winform窗口中,控件listview是无法设置行高的. 以加入一个imagelist(图片列表控件)实现行高的设置. ImageList imageList = new ImageList(); ...

  8. C#预编译指令

    近日工作涉及到于外部系统交互,对方提供接口:但是在双方系统未联调时,引用外部DLL,相关类实例化,提示异常错误(错误消息正常):后面操作无法进行,那如何写调试代码,即在调试时不运行某段代码,而在正式发 ...

  9. Orchard中文学习视频录制完成

    Orchard学习视频已登录百度传课: http://www.chuanke.com/3027295-124882.html http://pan.baidu.com/s/13zc0u 1.orcha ...

  10. std::function,std::bind复习

    #include <iostream> #include <functional>//std::bind返回函数对象 void fun1(int a, int b) { std ...