← Static tasks

205. Isomorphic Strings

leetcode easy

#array#csharp#easy#hash-table#leetcode#string

Task

Даны две строки s и t, определите, являются ли они изоморфными.

Две строки s и t являются изоморфными, если символы в s могут быть заменены для получения t.

Все вхождения одного символа должны быть заменены другим символом, сохраняя порядок символов. Два символа не могут отображаться в один и тот же символ, но один символ может отображаться сам на себя.

Пример:

Input: s = "egg", t = "add"

Output: true

C# solution

matched/original
public class Solution {
    public bool IsIsomorphic(string s, string t) {
        int[] mappingStoT = new int[256];
        int[] mappingTtoS = new int[256];
        for (int i = 0; i < s.Length; ++i) {
            char c1 = s[i];
            char c2 = t[i];
            if (mappingStoT[c1] == 0 && mappingTtoS[c2] == 0) {
                mappingStoT[c1] = c2;
                mappingTtoS[c2] = c1;
            } else if (!(mappingStoT[c1] == c2 && mappingTtoS[c2] == c1)) {
                return false;
            }
        }
        return true;
    }
}

C++ solution

auto-draft, review before submit
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public bool IsIsomorphic(string s, string t) {
        vector<int>& mappingStoT = new int[256];
        vector<int>& mappingTtoS = new int[256];
        for (int i = 0; i < s.size(); ++i) {
            char c1 = s[i];
            char c2 = t[i];
            if (mappingStoT[c1] == 0 && mappingTtoS[c2] == 0) {
                mappingStoT[c1] = c2;
                mappingTtoS[c2] = c1;
            } else if (!(mappingStoT[c1] == c2 && mappingTtoS[c2] == c1)) {
                return false;
            }
        }
        return true;
    }
}

Java solution

auto-draft, review before submit
import java.util.*;
import java.math.*;

// Auto-generated Java draft from the C# solution. Review API differences before LeetCode submit.
public class Solution {
    public boolean IsIsomorphic(String s, String t) {
        int[] mappingStoT = new int[256];
        int[] mappingTtoS = new int[256];
        for (int i = 0; i < s.length; ++i) {
            char c1 = s[i];
            char c2 = t[i];
            if (mappingStoT[c1] == 0 && mappingTtoS[c2] == 0) {
                mappingStoT[c1] = c2;
                mappingTtoS[c2] = c1;
            } else if (!(mappingStoT[c1] == c2 && mappingTtoS[c2] == c1)) {
                return false;
            }
        }
        return true;
    }
}

Explanation

Algorithm

1️⃣

Определите два словаря: mapping_s_t для отображения символов из строки s в символы строки t, и mapping_t_s для отображения символов из строки t в символы строки s.

2️⃣

Итеративно пройдитесь по символам строк s и t. Для каждого символа c1 из s и соответствующего c2 из t, если c1 нет в mapping_s_t и c2 нет в mapping_t_s, добавьте соответствующие отображения; если одно из отображений существует, проверьте, соответствует ли оно ожидаемому значению.

3️⃣

Если в процессе проверки какое-либо отображение неверно, верните false. Если все проверки пройдены успешно, верните true после окончания итерации по строкам.

😎