zoukankan      html  css  js  c++  java
  • [LeetCode] 205. Isomorphic Strings

    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.

    Example 1:

    Input: s = "egg", t = "add"
    Output: true
    

    Example 2:

    Input: s = "foo", t = "bar"
    Output: false

    Example 3:

    Input: s = "paper", t = "title"
    Output: true

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

    同构字符串。

    给两个字符串 s 和 t,判断他们是否互为同位字符串。同位字符串的定义是比如在 s 中有个字母“e”,在 t 中对应位置上有一个字母“g”,那么 s 中剩下所有的 e 应该在t对应位置上对应的是字母 g。

    两种思路。一个是用hashmap做,一个是用counting sort的思路做。

    遍历两个字符串,用hashmap存同样位置上s和t字母的对应关系。如果发觉有对不上的,就return false;遍历完的话就return true。

    时间O(n)

    空间O(n)

    JavaScript实现

     1 /**
     2  * @param {string} s
     3  * @param {string} t
     4  * @return {boolean}
     5  */
     6 var isIsomorphic = function(s, t) {
     7     if (s.length !== t.length) {
     8         return false;
     9     }
    10     if (s === t) {
    11         return true;
    12     }
    13     const obj1 = {};
    14     const obj2 = {};
    15     for (let i = 0; i < s.length; i++) {
    16         const letter = s[i];
    17         const tLetter = t[i];
    18         if (!obj2[tLetter]) {
    19             obj2[tLetter] = letter;
    20         }
    21         if (!obj1[letter]) {
    22             obj1[letter] = tLetter;
    23         }
    24         if (obj1[letter] !== tLetter || obj2[tLetter] !== letter) {
    25             return false;
    26         }
    27     }
    28     return true;
    29 };

    Java实现

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

    counting sort的思路跟hashmap很像,也是遍历字符串。用以上的example 1举例好了。当s遇到第一个字母e的时候,记录letters["e"] = "a",这样就记下了字母e和字母a的对应关系。如果发现有对不上的,就return false,直到遍历结束。

    时间O(n)

    空间O(1)

    JavaScript实现

     1 /**
     2  * @param {string} s
     3  * @param {string} t
     4  * @return {boolean}
     5  */
     6 var isIsomorphic = function(s, t) {
     7     if (s === t) {
     8         return true;
     9     }
    10     var len = s.length;
    11     var i = 1;
    12     if (len !== t.length) {
    13         return false;
    14     }
    15     while (i < len) {
    16         if (s.indexOf(s[i]) === t.indexOf(t[i])) {
    17             i++;
    18         } else {
    19             break;
    20         }
    21     }
    22     return i === len;
    23 };

    Java实现

     1 class Solution {
     2     public boolean isIsomorphic(String s, String t) {
     3         int[] sChars = new int[256];
     4         int[] tChars = new int[256];
     5         for (int i = 0; i < s.length(); i++) {
     6             if (sChars[s.charAt(i)] != tChars[t.charAt(i)]) {
     7                 return false;
     8             } else {
     9                 sChars[s.charAt(i)] = tChars[t.charAt(i)] = t.charAt(i);
    10             }
    11         }
    12         return true;
    13     }
    14 }

    相关题目

    205. Isomorphic Strings

    890. Find and Replace Pattern

    LeetCode 题目总结

  • 相关阅读:
    Java web ch02_5
    Java web ch02_4
    Java web ch02_3
    Java web ch02_9
    myeclipse和eclipse的区别以及优越性
    新的学年,迎来新的活力!
    Javaweb中的Tomcat服务器(简单了解)
    一切都是对象
    java中被隐藏的具体实现
    SSL与TLS的区别以及介绍
  • 原文地址:https://www.cnblogs.com/cnoodle/p/11762616.html
Copyright © 2011-2022 走看看