solution.md 4.4 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
# 同构字符串
<p>给定两个字符串 <em><strong></strong></em>和 <strong><em>t</em></strong>,判断它们是否是同构的。</p>

<p>如果 <em><strong></strong></em>中的字符可以按某种映射关系替换得到 <strong><em></em></strong>,那么这两个字符串是同构的。</p>

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

<p> </p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong><strong><em>s</em></strong> = <code>"egg", </code><strong><em>t = </em></strong><code>"add"</code>
<strong>输出:</strong>true
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong><strong><em>s</em></strong> = <code>"foo", </code><strong><em>t = </em></strong><code>"bar"</code>
<strong>输出:</strong>false</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入:</strong><strong><em>s</em></strong> = <code>"paper", </code><strong><em>t = </em></strong><code>"title"</code>
<strong>输出:</strong>true</pre>

<p> </p>

<p><strong>提示:</strong></p>

<ul>
	<li>可以假设 <em><strong></strong></em><strong><em>t </em></strong>长度相同。</li>
</ul>

<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
    Solution sol;
    string a = "egg";
    string b = "add";

    bool res;

    res = sol.isIsomorphic(a, b);
    cout << res;
    return 0;
}
```

## 答案
```cpp
class Solution
{
public:
    bool isIsomorphic(string s, string t)
    {
        if (s.size() != t.size())
            return false;
        for (int i = 0; i < s.size(); i++)
        {
            int pos = s.find_first_of(s[i], i + 1);
            while (pos != -1)
            {
                if (t[i] == t[pos + 1])
                {
                    s.erase(pos, 1);
                    t.erase(pos, 1);
                    pos = s.find_first_of(s[i], pos + 1);
                }
                else
                    return false;
            }
        }
        for (int i = 0; i < t.size(); i++)
        {
            int pos = t.find_first_of(t[i], i + 1);
            while (pos != -1)
            {
                if (s[i] == s[pos + 1])
                {
                    s.erase(pos, 1);
                    t.erase(pos, 1);
                    pos = t.find_first_of(t[i], pos + 1);
                }
                else
                    return false;
            }
        }
        return true;
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    bool isIsomorphic(string s, string t)
    {
        if (s.size() != t.size())
            return false;
        for (int i = 0; i < s.size(); i++)
        {
            if (s.find(s[i]) != t.find(t[i]))
                return false;
        }
        return true;
    }
};
```

### B
```cpp
class Solution
{
public:
    bool isIsomorphic(string s, string t)
    {
        if (s.size() != t.size())
            return false;
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == s[i + 1])
            {
                s.erase(i + 1, 1);
                i--;
            }
        }
        for (int i = 0; i < t.size(); i++)
        {
            if (t[i] == t[i + 1])
            {
                t.erase(i + 1, 1);
                i--;
            }
        }
        if (s.size() != t.size())
            return false;
        for (int i = 0; i < s.size(); i++)
        {
            int pos = s.find_first_of(s[i], i + 1);
            while (pos != -1)
            {
                if (t[i] == t[pos])
                {
                    s.erase(pos, 1);
                    t.erase(pos, 1);
                    pos = s.find_first_of(s[i], pos + 1);
                }
                else
                    return false;
            }
        }
        return true;
    }
};
```

### C
```cpp
class Solution
{
public:
    bool isIsomorphic(string s, string t)
    {
        unordered_map<char, char> map1;
        unordered_map<char, char> map2;
        int n = s.size();
        for (int i = 0; i < n; i++)
        {
            char x = s[i], y = t[i];
            if ((map1.count(x) && map1[x] != y) || (map2.count(y) && map2[y] != x))
                return false;
            map1[x] = y;
            map2[y] = x;
        }
        return true;
    }
};
```