关关的刷题日记 38 – Leetcode 205. Isomorphic Strings

关关的刷题日记38 – Leetcode 205. 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.

Note: You may assume both s and t have the same length.

题目的意思是判断s和t是否是同类型的词,题目中只说不能出现多对一的情况,比如说Given "aa", "ab", return false. 写了段代码测试之后发现一对多也不行,比如说:Given "ab", "aa", return false。所以又在原来的基础上加了个set,最后再判断下map和set的字母数是否相等。


class Solution {
public:
    bool isIsomorphic(string s, string t) {
        map<char,char>m;
        set<char>p;
        for(int i=0; i<s.size();i++)
        {
            p.insert(t[i]);
            if(m.find(s[i])!=m.end())
            {
                if(m[s[i]]!=t[i])
                    return false;
            }
            else
                m[s[i]]=t[i];
        }
        if(m.size()!=p.size())
            return false;
        return true;
    }
};

人生易老,唯有陪伴最长情,加油!

以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang)。 同时请,关注我们的公众号,获取最新关于专知以及人工智能的资讯、技术、算法等内容。扫一扫下方关注我们的微信公众号。

图片

展开全文
相关主题
Top
微信扫码咨询专知VIP会员