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 
  1. public class Solution {
  2. public boolean isIsomorphic(String s, String t) {
  3. if(s==null || t ==null)
  4. return false;
  5. if(s.length()!=s.length())
  6. return false;
  7. if(s==null && t == null)
  8. return true;
  9. HashMap<Character,Character> map = new HashMap<Character,Character>();
  10. for(int i = 0;i< s.length();i++){
  11. char c1 = s.charAt(i);
  12. char c2 = t.charAt(i);
  13. Character c = getKey(map,c2);
  14. if(c !=null && c!=c1)
  15. return false;
  16. else if(map.containsKey(c1)){
  17. if(c2!=map.get(c1))
  18. return false;
  19. }else{
  20. map.put(c1,c2);
  21. }
  22. }
  23. return true;
  24. }
  25.  
  26. public Character getKey(HashMap<Character,Character> map,Character target){
  27. for(Map.Entry<Character,Character> entry:map.entrySet()){
  28. if(entry.getValue().equals(target)){
  29. return entry.getKey();
  30. }
  31. }
  32. return null;
  33. }
  34. }

或者

  1. public class Solution {
  2. public boolean isIsomorphic(String s, String t) {
  3. if(s==null || t ==null)
  4. return false;
  5. if(s.length()!=s.length())
  6. return false;
  7. if(s==null && t == null)
  8. return true;
  9. HashMap<Character,Character> map = new HashMap<Character,Character>();
  10. for(int i = 0;i< s.length();i++){
  11. char c1 = s.charAt(i);
  12. char c2 = t.charAt(i);
  13. Character c = map.get(c1);
  14. // key c1 value c2
  15. if(map.containsKey(c1)){
  16. if(map.get(c1).equals(c2))
  17. continue;
  18. else
  19. return false;
  20. }else{
  21. if(!map.containsValue(c2))
  22. map.put(c1,c2);
  23. else
  24. return false;
  25.  
  26. }
  27.  
  28. }
  29. return true;
  30. }
  31.  
  32. }

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

  1. public class Solution {
  2. public boolean isIsomorphic(String s, String t) {
  3. if(s==null || t ==null)
  4. return false;
  5. if(s.length()!=s.length())
  6. return false;
  7. if(s==null && t == null)
  8. return true;
  9. int[] m = new int[512];
  10. for (int i = 0; i < s.length(); i++) {
  11. if (m[s.charAt(i)] != m[t.charAt(i)+256])
  12. return false;
  13. m[s.charAt(i)] = m[t.charAt(i)+256] = i+1;
  14. }
  15. return true;
  16. }
  17.  
  18. }

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. Datawarehouse

  2. PB中掉用Run以后,等Run的程序关闭以后才会执行后边的语句

    OleObject wsh integer li_rc CONSTANT integer MAXIMIZED = CONSTANT integer MINIMIZED = CONSTANT integ ...

  3. 详谈socket请求Web服务器过程

    最开始我们需要明白一件事情,因为这是这篇文章的前提: HTTP协议只是一个应用层协议,它底层是通过TCP进行传输数据的.因此,浏览器访问Web服务器的过程必须先有“连接建立”的发生. 而有人或许会问: ...

  4. Using sql azure for Elmah

    The MSDN docs contain the list of T-SQL that is either partially supported or not supported.  For ex ...

  5. Codeforces Beta Round #69 (Div. 1 Only) C. Beavermuncher-0xFF 树上贪心

    题目链接: http://codeforces.com/problemset/problem/77/C C. Beavermuncher-0xFF time limit per test:3 seco ...

  6. sentinel.conf配置

    1.常用的配置 port 26379 # sentinel announce-ip <ip> # sentinel announce-port <port> dir /tmp ...

  7. Promises与Javascript异步编程

    Promises与Javascript异步编程 转载:http://www.zawaliang.com/2013/08/399.html 在如今都追求用户体验的时代,Ajax应用真的是无所不在.加上这 ...

  8. NYOJ-102 次方求模 AC 分类: NYOJ 2014-02-06 18:53 184人阅读 评论(0) 收藏

    地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=102 //a^b mod c=(a mod c)^b mod c很容易设计出一个基于二分的递归 ...

  9. [nowCoder] 二进制中1的个数

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示.     class Solution { public: int NumberOf1(int n) { ; while(n) ...

  10. AngularJs学习笔记--html compiler

    原文再续,书接上回...依旧参考http://code.angularjs.org/1.0.2/docs/guide/compiler 一.总括 Angular的HTML compiler允许开发者自 ...