【LeetCode 205】关关的刷题日记38 Isomorphic Strings

时间:2022-05-07
本文章向大家介绍【LeetCode 205】关关的刷题日记38 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)。