• Leetcode题目: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

    题目解答:判断两个单词是否是同构的。需要两个map,分别控制s到t和t到s的映射关系。

    代码如下:

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

  • 相关阅读:
    Java补漏(一)
    PHP实现程序单例执行
    zabbix 配置外部邮件server发送邮件报警
    HTML+JavaScript实现链式运动特效
    对思归者的建议
    去除Notepad++打开文件后文字下面出现红色波浪线的问题
    ANSI是什么?
    Eclipse各版本代号一览表以及官网上有很多版本的eclipse,下载哪个版本比较合适呢?
    Java语言的发展史
    win10 64位JLink v8固件丢失修复总结
  • 原文地址:https://www.cnblogs.com/CodingGirl121/p/5417710.html
Copyright © 2020-2023  润新知