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 ...
随机推荐
- loj#2838 「JOISC 2018 Day 3」比太郎的聚会
分析 预处理每个点的前根号小的距离 对于每次询问删除点小于根号则已经处理好 否则直接暴力dp即可 代码 #include<bits/stdc++.h> using namespace st ...
- Gradle查看Project中的所有 Task
查看Project中所有的Task:$ gradle tasks 查看Project中所有的properties:$ gradle properties 如: 参照了: https://www.jia ...
- 深入RESTful无状态原则
目录 目录 前言 无状态原则 Web服务的状态 基于状态的Web服务 基于无状态的Web服务 总结两者的区别 前言 在上篇RESTful基础知识中整体的介绍了RESTful架构设计思想的框架,在往后的 ...
- iframe的基础应用
点击top.html里面的按钮,刷新left.html右边的内容 <a href="left.html?id=11111&k=5&b=4" target=& ...
- python可变数据类型和不可变数据类型
1.可变数据类型:在id不变的情况下,value可改变(列表和字典是可变类型,但是字典中的key值必须是不可变类型) 2.不可变数据类型:value改变,id也跟着改变.(数字,字符串,布尔类型,都是 ...
- jmeter 非GUI执行测试,没有响应数据保存到jtl文件办法
估计是jmeter为了减轻客户机负担,就没又默认把这些信息保存,如果想要保存,也可以,需要做出如下配置: 修改bin目录下的user.properties文件,追加配置: jmeter.save.sa ...
- [Linux] 027 RPM 包与 源码包的区别
1. 区别 安装之前的区别: 概念上的区别 安装之后的区别: 安装位置不同 2. RPM 包安装位置 安装在默认位置中 RPM 包默认安装路径 明细 /ect 配置文件安装目录 /usr/bin/ 可 ...
- Netty之Page级别的内存分配
Page 级别的内存分配: 之前我们介绍过, netty 内存分配的单位是chunk, 一个chunk 的大小是16MB, 实际上每个chunk, 都以双向链表的形式保存在一个chunkList 中, ...
- Netty内存管理器ByteBufAllocator及内存分配
ByteBufAllocator 内存管理器: Netty 中内存分配有一个最顶层的抽象就是ByteBufAllocator,负责分配所有ByteBuf 类型的内存.功能其实不是很多,主要有以下几个重 ...
- LeetCode10 Indexed tree
Binary Indexed Tree(Fenwick tree): 是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有元素之和,但是每次只能修改一个元素的值:经过简单修 ...