leetcode python3 简单题205. Isomorphic Strings
1.编辑器
我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接
2.第二百零五题
(1)题目
英文:
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.
中文:
给定两个字符串 s 和 t,判断它们是否是同构的。
如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。
所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/isomorphic-strings
(2)解法
① 对于同构字符串,两个字符串中第一个出现的字符对应的index必须相同。
(耗时:48ms,内存:14.3M)
class Solution:def isIsomorphic(self, s: str, t: str) -> bool:return [*map(s.index, s)] == [*map(t.index, t)]
注意:
1.map输出输出的是一个对象地址,如果要输出具体的数值,则可以使用:
① *map(…),也可以外加一个[],输出列表
② list(map(…)),直接输出列表
② 使用dict,分情况讨论,这个的流程和题目介绍的流程相似
(耗时:44ms,内存:13.9M)
class Solution:def isIsomorphic(self, s: str, t: str) -> bool:dic = {}s = list(s)t = list(t)for i in range(len(s)):if t[i] not in dic:dic[t[i]] = s[i]if len(dic.values()) != len(set(dic.values())):return Falsefor i in range(len(s)):t[i] = dic[t[i]] return t == s
注意:
1.分情况讨论详述:
① 首先:
for i in range(len(s)):if t[i] not in dic:dic[t[i]] = s[i]
这段是拿到t和s中第一次出现字符的对应关系,如果t中字符已有出现在dic中,则不会新添加了,这样可以限制后面只有是同构的情况才能使t==s,也就是t中有字符重复几次,s中的相应位置的字符也要重复几次,否则t!=s
② 然后:
如果dic中有重复的value,则表明s中有重复字符,而在t中没有对应着重复,直接输出False
③ 如果②是满足的,则通过下面代码来判断s中的重复模式是否与t相同,如果相同,则输出True
leetcode python3 简单题205. Isomorphic Strings相关推荐
- leetcode python3 简单题14. Longest Common Prefix
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第十四题 (1)题目 英文: Write a function to find th ...
- leetcode python3 简单题225. Implement Stack using Queues
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...
- leetcode python3 简单题70. Climbing Stairs
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第七十题 (1)题目 英文: You are climbing a stair ca ...
- leetcode python3 简单题69. Sqrt(x)
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第六十九题 (1)题目 英文: Implement int sqrt(int x). ...
- leetcode python3 简单题67. Add Binary
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第六十七题 (1)题目 英文: Given two binary strings, ...
- leetcode python3 简单题53. Maximum Subarray
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十三题 (1)题目 英文: Given an integer array num ...
- leetcode python3 简单题58. Length of Last Word
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十八题 (1)题目 英文: Given a string s consists ...
- leetcode python3 简单题234. Palindrome Linked List
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十四题 (1)题目 英文: Given a singly linked li ...
- leetcode python3 简单题232. Implement Queue using Stacks
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十二题 (1)题目 英文: Implement the following ...
最新文章
- 模拟退火 HDU - 2899 Strange Function
- 深度学习在小分子药物研发中的应用
- python3 日志检索异常抛出异常 raise KeyError(key),KeyError: 'formatters'
- centOS安装python
- Spring Cloud Config入门(本地配置)
- mysql 存储过程 查询结果 循环_在存储过程中从查询结果集中怎么循环
- 打基础一定要吃透这12类 Python 内置函数
- 补习系列(4)-springboot 参数校验详解
- linux充电时熄屏时间,充电时玩手机会损害电池吗
- Auto.js 全命令整理(一) 数据获取专题
- Error:java: Invalid additional meta-data in ‘META-INF/spring-configuration-metadata.json‘: End of in
- 多数iPhone应用程序的不足之处
- 蓝桥杯“基础练习: 十六进制转十进制
- java写入txt文件_java实现写入并保存txt文件的代码详解
- 基于EMC的共模干扰与差模干扰以及抑制方法
- 北上广等一线城市IT岗位已接近饱和?
- 上面两点下面一个三角形_章勤琼:三角形内角和180该如何说明 ——小学数学中的合情推理和演绎推理...
- 厉害了!99行代码实现《冰雪奇缘》
- python 行列分不清
- Java分布式全局ID(一)
热门文章
- python定时器 是线程吗_定时器中断线程python
- @value注解取不到值_教学笔记:Java注解及自定义注解示例
- 静态成员变量以及静态成员函数
- 华为MDC调试工具使用介绍
- 树与图的深度优先遍历
- NOI Online能力测试2视频版,让我们看看出题专家怎么说!(入门组)
- Python——(Anaconda+PyCharm)Youki的Python环境配置笔记(Windows版本)(legacy)~
- Python考试基础知识
- InteliJ Idea通过maven创建webapp
- hihocoder1241 Best Route in a Grid