Python3解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.
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 Note:
You may assume both s and t have the same length.
思路:
两个字符串对应位置上的字母要具有一一对应的关系,即假如s[1]=s,t[1]=t,则s对应的就是t,如果后面s[i]=s,则t[i]也必须为 t,若t[i]为其他字母,则判定为非同形态。
这种一一对应关系,首先考虑字典
代码:
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
table = dict()
for i in range(len(s)):
if s[i] not in table:
table[s[i]] = t[i]
elif table[s[i]] != t[i]:
return False
return len(table) == len(set(table.values()))
如果字典中没有相应键值对,那么就在字典中加入该键值对;如果有该键值对,判断一下该元素是否和原存储的键值对相同,不相同则肯定是非同形态,如果相同则是到目前为止为同形态。
有一种特殊情况,就是不同的键,对应了相同的值,如s='ab',t=‘aa’,出现这种情况代表是非同形态的,通过判断键、值的个数来判断是否出现这种情况。
Python3解leetcode Isomorphic Strings的更多相关文章
- [LeetCode] Isomorphic Strings
Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Python3解leetcode N-ary Tree Level Order Traversal
问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- Python3解leetcode Rotate Array
问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...
- Python3解leetcode Linked List Cycle
问题描述: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given link ...
- Python3解leetcode Single Number
问题描述: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...
- Python3解leetcode Best Time to Buy and Sell Stock II
问题描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- Python3解leetcode Same TreeBinary Tree Level Order Traversal II
问题描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
- Python3解leetcode Same Tree
问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...
随机推荐
- Getting Started Tutorial from msdn
Getting Started Tutorial The topics contained in this section are intended to give you quick exposur ...
- 132、TensorFlow加载模型
# The tf.train.Saver对象不仅保存变量到checkpoint文件 # 它也恢复变量,当你恢复变量的时候,你就不必须要提前初始化他们 # 列如如下的代码片段解释了如何去调用tf.tra ...
- Java 内部类“覆盖"
Think in Java P269 如果子类中的内部类和父类中内部类一样,这好像子类内部类“覆盖”了父类的内部类,但其实没有代表着什么. public class BigEgg extends E ...
- ElasticSearch2.2.0安装
一.ElasticSearch2.2.0安装 1.下载ElasticSearch2.2.0安装包 https://download.elastic.co/elasticsearch/elasticse ...
- python自定义异常实例详解
python自定义异常实例详解 本文通过两种方法对Python 自定义异常进行讲解,第一种:创建一个新的exception类来拥有自己的异常,第二种:raise 唯一的一个参数指定了要被抛出的异常 1 ...
- 涛涛的小马甲 Android之Handler机制
首先需要了解一个基本的概念ANR:Application not response 即应用程序无响应,也就是俗话说的死机. 出现Anr的原因是: 主线程需要做很多重要的事情,响应点击事件,更新UI如果 ...
- Entity Framework Code First (五)Fluent API - 配置关系 转载 https://www.cnblogs.com/panchunting/p/entity-framework-code-first-fluent-api-configuring-relationships.html
上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ...
- 使用rdb文件进行redis数据迁移--python脚本
查找了一些redis迁移的方法,一般做法就是 1. 从源数据库把rdb文件保存,然后传到新的主机上,启动新的redis即可 2. 把新的redis当做源数据库的slave,同步数据 今天开发提了一个测 ...
- Colourful Rectangle【扫描线】
题目链接 很明显的可以发现是一个扫描线的问题,但是怎么处理区域呢,发现只有三种颜色,也就是最多也就是7种状态,那么我们可以进行一个状态压缩即可. 但是,在向上pushup的时候,存在我们要以子树的状态 ...
- Maven安装、配置环境变量
一.首先在官网下载安装maven 1.进入官网 2.找到下载位置 3.点进去后是最新版的,若需要最新版就下这个,需要旧版本接着往下滑 4.下载历史版本 (1)点击"archives" ...