[LeetCode] 205. Isomorphic Strings

Given two strings s and tdetermine if they are isomorphic.

Two strings s and t 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

Constraints:

  • 1 <= s.length <= 5 * 104
  • t.length == s.length
  • s and t consist of any valid ascii character.

同构字符串。

给定两个字符串 s 和 t ,判断它们是否是同构的。

如果 s 中的字符可以按某种映射关系替换得到 t ,那么这两个字符串是同构的。

每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/isomorphic-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

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

两种实现方式,思路本质上是一样的。一个是用 hashmap 做,一个是用 counting sort 的办法做。

遍历两个字符串,用 hashmap 存同一个下标位置上 s 和 t 字母的对应关系。如果发觉有对不上的,就 return false;遍历完的话就 return true。比如在某个下标处 s 的字符是 c1,t 的字符是 c2,我们只能得到 c1 -> c2 的单向关系,我们是不能同时得到 c2 -> c1 这个结论的。所以这道题在写代码的时候用数组更方便。

时间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             char c1 = s.charAt(i);
 7             char c2 = t.charAt(i);
 8             if (sChars[c1] != tChars[c2]) {
 9                 return false;
10             } else {
11                 sChars[c1] = tChars[c2] = c2;
12             }
13         }
14         return true;
15     }
16 }

 

相关题目

205. Isomorphic Strings

890. Find and Replace Pattern

LeetCode 题目总结

posted @ 2019-10-30 02:15  CNoodle  阅读(188)  评论(0编辑  收藏  举报