---
# System prepended metadata

title: \#205 Isomorphic Strings
tags: [leetcode, leetcode-easy]

---

# \#205 Isomorphic Strings
## *檢測s和t兩字串是否為"同構字"(以特定對應替換字母後, s==t)*
## Log
 - build 20210527 by syhuang

## xxx
 - xxx
```javascript=
```
## 初見
 - 用map存放每個字母對應的新字母, 迴圈判斷兩字串裡的每個字元替換後是否相同, 並且要交換字串再跑一次檢查, 以免有"兩個不同字母對應到相同字母"的情況
```javascript=
var isIsomorphic = function(s, t) {
    if(s.length!=t.length) return false;//先決條件:s和t必定等長度
    var charMap = {};
    var sary = s.split('');
    var tary = t.split('');
    var c1 = true;
    var c2 = true;
    for(var i=0; i<s.length; i++){
        if(!charMap[sary[i]]) charMap[sary[i]] = tary[i];
        if(charMap[sary[i]] != tary[i]) c1=false;
    }
    charMap = {};
    for(var i=0; i<t.length; i++){
        if(!charMap[tary[i]]) charMap[tary[i]] = sary[i];
        if(charMap[tary[i]] != sary[i]) c2=false;
    }
    return c1 && c2;
};
```
## 備註
 - [另一種特殊解法](https://leetcode.com/problems/isomorphic-strings/discuss/57796/My-6-lines-solution)
## 參考
###### tags: `leetcode`, `leetcode-easy`