# 205.Isomorphic Strings

**205.Isomorphic Strings**

难度:Easy

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

如果 s 中的字符可以被替换得到 t ，那么这两个字符串是同构的。

所有出现的字符都必须用另一个字符替换，同时保留字符的顺序。两个字符不能映射到同一个字符上，但字符可以映射自己本身。

```
示例 1:

输入: s = "egg", t = "add"
输出: true
示例 2:

输入: s = "foo", t = "bar"
输出: false
示例 3:

输入: s = "paper", t = "title"
输出: true
说明:
你可以假设 s 和 t 具有相同的长度。
```

主要是判斷，在一個字符串中字符相等的位置，在另一個字符串中是否也相等。\
構建了一個數組，第一次出現不同的數記爲1，第二次爲2，以此類推，看最後得到的兩個數組是否相等。

```
class Solution {
vector<int> toNum(string a)
{
    int start=0;
    vector<int>res;
    unordered_map<char,int>order;
   for(auto c:a)
   {
       if(order.count(c))
           res.push_back(order[c]);
       else
       {
           order[c]=++start;
           res.push_back(start);
   }
   }
    return res;
        
    }
    

public:
    bool isIsomorphic(string s, string t) {
        if(s.length()!=t.length())return false;
        unordered_set<int> order;
        for(int i=0;i<s.length();i++)
            order.insert(i);
         return toNum(s) == toNum(t);

    }
};
```

> 执行用时 :32 ms, 在所有 C++ 提交中击败了13.74%的用户\
> 内存消耗 :15.4 MB, 在所有 C++ 提交中击败了5.09%的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/205.isomorphic_strings.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
