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 ...
随机推荐
- statistic_action
方差variance 统计中的方差(样本方差)是每个样本值与全体样本值的平均数之差的平方值的平均数.V 离差平方和(Sum of Squares of Deviations)是各项与平均项之差的平方的 ...
- Git014--Rebase
Git--Rebase 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b00 ...
- Learning OSG programing---osgAnimation(1)
osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) { ...
- python 转义json串
import re body=r''' {"id":${BXbiztripinfoID_10},"msg":"1234测试审批${__UUID}&qu ...
- [功能集锦] 002 - mysql查询数据库字典+导出+样式一键整合至excel
写在前面: 因为工作时候经常遇到半路接手项目的情况,由于年代久远,数据库字典这块经常缺失.故写此篇,以便复用,也希望对大家有点帮助. 随笔内容不高级,如有不妥,不吝指正. 20190730-加了一些简 ...
- CSS的置换和非置换元素
一个来自面试的坑. 面试的时候考官先问了行内元素和块级元素的区别,这个不难理解.然后一脚就踩进了,置换元素的坑.例如img就是行内置换元素,这种行内元素是可以设置宽高的. 什么是置换元素 一个内容不受 ...
- 74.Maximal Rectangle(数组中的最大矩阵)
Level: Hard 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle con ...
- JS的video在手机上有些手机能播放,而有些不能原因
一,一开始我video是这样写的,直接给地址,然后有很多手机却不能播放,或者说卡在一开始的页面,是什么原因呢? <video id='video' > <source src='xx ...
- 【问题解决方案】git中的文件的重命名
环境: win7 git bash+GitHub 问题: 如果直接在文件夹中手动重命名,Git会判定为删除了旧文件,添加了新文件 版本回退后使用mv命令进行重命名 $ git reset --hard ...
- 【知识强化】第三章 存储系统 3.5 双口RAM和多模块存储器
下面我们进入双端口RAM和多模块存储器的学习.这是提高我们的存储器的访存速度的一些措施. 我们之前已经讲过我们的主存和CPU是进行连接的,那么这就导致了一个问题就是说,随着我们现代科技的发展,计算机的 ...