前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【LeetCode 205】关关的刷题日记38 Isomorphic Strings

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

作者头像
WZEARW
发布2018-04-10 16:39:25
4990
发布2018-04-10 16:39:25
举报
文章被收录于专栏:专知专知

关关的刷题日记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的字母数是否相等。

代码语言:javascript
复制
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)。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-11-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 专知 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档