[LeetCode] 205. Isomorphic Strings
同位字符串。给两个字符串s和t,判断他们是否互为同位字符串。同位字符串的定义是比如在s中有个字母“e”,在t中对应位置上有一个字母“g”,那么s中剩下所有的e应该在t对应位置上对应的是字母g。例子如下 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 两种思路。一个是用hashmap做,一个是用counting sort的思路做。 遍历两个字符串,用hashmap存同样位置上s和t字母的对应关系。如果发觉有对不上的,就return false;遍历完的话就return true。 时间O(n) 空间O(n) 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