[LeetCode] 205. Isomorphic Strings

来源:互联网 发布:iphone笔记软件 编辑:程序博客网 时间:2024/05/29 02:49

思路:
拿两个哈希. 一个哈希存储s和t中字符的映射关系, 另外一个存储t的字符是否已经被映射过了, 如果用过一次了, 就不能再用了, 要是t中的一种字符被多个s中的字符映射, 那么也是错误的.

bool isIsomorphic(string s, string t) {    if (s.length() != t.length())        return false;    char hash[256] = {0};    bool mapped[256] = {0};    for (int i = 0; i < s.length(); i++) {        if (hash[s[i]]) {            if (hash[s[i]] != t[i])                return false;        }        else {            if (mapped[t[i]])                return false;            hash[s[i]] = t[i];            mapped[t[i]] = true;        }    }    return true;}
0 0
原创粉丝点击