LeetCode205. Isomorphic Strings 解题小结

Posted 医生工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode205. Isomorphic Strings 解题小结相关的知识,希望对你有一定的参考价值。

题目:Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

For example,
Given "egg""add", return true.

Given "foo""bar", return false.

Given "paper""title", return true.

设置两个cs和ct数组,记录目前遍历字符串s和t的下标i+1,如果两者不相等,返回false。

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int cs[255] = {0}, ct[255] = {0};
        for (int i = 0; i < s.size(); ++i){
            if (cs[s[i]] != ct[t[i]])
                return false;
            else{
                cs[s[i]] = i + 1;
                ct[t[i]] = i + 1;
            }
        }
        return true;
    }
};

 

以上是关于LeetCode205. Isomorphic Strings 解题小结的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 205. Isomorphic Strings

LeetCode 205 Isomorphic Strings

LeetCode 205 Isomorphic Strings

[LeetCode] 205 Isomorphic Strings

LeetCode 205. Isomorphic Strings

LeetCode205. Isomorphic Strings 解题小结