鱼C论坛

 找回密码
 立即注册
查看: 919|回复: 6

[技术交流] 【朱迪的LeetCode刷题笔记】205. Isomorphic Strings 同构字符串 #Easy #Python

[复制链接]
发表于 2023-5-23 12:51:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Judie 于 2023-5-23 02:21 编辑

Given two strings s and t, determine if they are isomorphic.

Two strings s and t 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.

Example 1:
Input: s = "egg", t = "add"
Output: true

Example 2:
Input: s = "foo", t = "bar"
Output: false

Example 3:
Input: s = "paper", t = "title"
Output: true


Constraints:
1 <= s.length <= 5 * 104
t.length == s.length
s and t consist of any valid ascii character.
class Solution(object):
    def isIsomorphic(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
class Solution {
public:
    bool isIsomorphic(string s, string t) {
        
    }
};

Judy
class Solution(object):
    def isIsomorphic(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        d = {}
        for x, y in zip(s, t):
            if x not in d:
                if y not in d.values():
                    d[x] = y
                else:
                    return False
            elif d[x] != y:
                return False

        return True
        

Gray
c++ bad version?
#include <iostream>
#include <map>

#include <algorithm>

class Solution {
    
public:
    bool isIsomorphic(string s, string t) {
        map<char,char> dict;
        
        for(int i = 0; i < s.size(); i++){
            vector<char> value;
            for(auto it = dict.cbegin(); it != dict.cend(); ++it){
                value.push_back(it->second);
            }
            if (dict.find(s[i])!=dict.end()){
                s[i] = dict[s[i]];
            }else {
                cout << "cycle" << endl;
                for(int j = 0; j < value.size();j++){
                    if(value[j] == t[i]){
                        return false;
                    }
                }
                dict[s[i]] = t[i];
                s[i] = t[i];
            }
        }
        if(s == t){
            return true;
        }else {
            return false;
        }

    }
};

c++ better version?
#include <iostream>
#include <map>

#include <algorithm>

class Solution {
    
public:
    bool isIsomorphic(string s, string t) {
        map<char,char> dict;
        
        for(int i = 0; i < s.size(); i++){
            vector<char> value;

            if (dict.find(s[i])!=dict.end()){
                s[i] = dict[s[i]];
            }else {
                for(auto it = dict.cbegin(); it != dict.cend(); ++it){
                if(it->second == t[i]){
                   return false;
                }
            }
                dict[s[i]] = t[i];
                s[i] = t[i];
            }
        }
        if(s == t){
            return true;
        }else {
            return false;
        }

    }
};

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-23 14:31:16 | 显示全部楼层
有趣的同构
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-23 14:32:23 | 显示全部楼层

谢谢!你也刷leetcode嘛!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-23 14:36:55 | 显示全部楼层
Judie 发表于 2023-5-23 14:32
谢谢!你也刷leetcode嘛!

是的,但只是偶尔刷,毕竟我需要花更多的时间在数学上,目前能够独立完成的水平仅限于一部分简单题,因为有些简单题的知识点没有接触过。主要使用C语言和Python
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-23 14:38:42 | 显示全部楼层
yinda_peng 发表于 2023-5-23 01:36
是的,但只是偶尔刷,毕竟我需要花更多的时间在数学上,目前能够独立完成的水平仅限于一部分简单题,因为 ...

了解!我打算从今天开始多刷刷
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-23 14:39:54 | 显示全部楼层
Judie 发表于 2023-5-23 14:38
了解!我打算从今天开始多刷刷

加油!祝早日offer
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-23 14:40:38 | 显示全部楼层
yinda_peng 发表于 2023-5-23 01:39
加油!祝早日offer

谢谢!其实是为了学习算法 准备考试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-16 16:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表