鱼C论坛

 找回密码
 立即注册
查看: 870|回复: 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.

  1. class Solution(object):
  2.     def isIsomorphic(self, s, t):
  3.         """
  4.         :type s: str
  5.         :type t: str
  6.         :rtype: bool
  7.         """
复制代码

  1. class Solution {
  2. public:
  3.     bool isIsomorphic(string s, string t) {
  4.         
  5.     }
  6. };
复制代码


Judy
  1. class Solution(object):
  2.     def isIsomorphic(self, s, t):
  3.         """
  4.         :type s: str
  5.         :type t: str
  6.         :rtype: bool
  7.         """
  8.         d = {}
  9.         for x, y in zip(s, t):
  10.             if x not in d:
  11.                 if y not in d.values():
  12.                     d[x] = y
  13.                 else:
  14.                     return False
  15.             elif d[x] != y:
  16.                 return False

  17.         return True
  18.         
复制代码


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

  3. #include <algorithm>

  4. class Solution {
  5.    
  6. public:
  7.     bool isIsomorphic(string s, string t) {
  8.         map<char,char> dict;
  9.         
  10.         for(int i = 0; i < s.size(); i++){
  11.             vector<char> value;
  12.             for(auto it = dict.cbegin(); it != dict.cend(); ++it){
  13.                 value.push_back(it->second);
  14.             }
  15.             if (dict.find(s[i])!=dict.end()){
  16.                 s[i] = dict[s[i]];
  17.             }else {
  18.                 cout << "cycle" << endl;
  19.                 for(int j = 0; j < value.size();j++){
  20.                     if(value[j] == t[i]){
  21.                         return false;
  22.                     }
  23.                 }
  24.                 dict[s[i]] = t[i];
  25.                 s[i] = t[i];
  26.             }
  27.         }
  28.         if(s == t){
  29.             return true;
  30.         }else {
  31.             return false;
  32.         }

  33.     }
  34. };
复制代码


c++ better version?
  1. #include <iostream>
  2. #include <map>

  3. #include <algorithm>

  4. class Solution {
  5.    
  6. public:
  7.     bool isIsomorphic(string s, string t) {
  8.         map<char,char> dict;
  9.         
  10.         for(int i = 0; i < s.size(); i++){
  11.             vector<char> value;

  12.             if (dict.find(s[i])!=dict.end()){
  13.                 s[i] = dict[s[i]];
  14.             }else {
  15.                 for(auto it = dict.cbegin(); it != dict.cend(); ++it){
  16.                 if(it->second == t[i]){
  17.                    return false;
  18.                 }
  19.             }
  20.                 dict[s[i]] = t[i];
  21.                 s[i] = t[i];
  22.             }
  23.         }
  24.         if(s == t){
  25.             return true;
  26.         }else {
  27.             return false;
  28.         }

  29.     }
  30. };
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> 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-5-24 04:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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