问题描述:

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 and 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的更多相关文章

  1. [LeetCode] Isomorphic Strings

    Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...

  2. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  3. 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 ...

  4. Python3解leetcode Rotate Array

    问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...

  5. 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 ...

  6. Python3解leetcode Single Number

    问题描述: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...

  7. 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 ...

  8. 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 ...

  9. Python3解leetcode Same Tree

    问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...

随机推荐

  1. Linux学习篇(二)-软件包管理器、Yum 软件仓库

    红帽软件包管理器 在红帽软件包管理器(rpm)公布之前,Linux 系统软件的安装只能采取"源码包"的方式安装,需要自行编译源码并解决许多依赖关系,所以软件的安装.升级.卸载的难度 ...

  2. scrapy-redis源码浅析

    原文链接 前言 分析这个项目的源码原因是需要有去重过滤,增量爬取两个功能,而scrapy-redis项目已经帮我们实现了,想看看他是怎么实现的.这里只贴出部分主要代码,查看时请打开源码对照,笔记有点长 ...

  3. mariadb(一)基础

    一.数据库介绍 1.什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方法来 ...

  4. 【转载】Spring boot学习记录(二)-配置文件解析

    前言:本系列文章非本人原创,转自:http://tengj.top/2017/04/24/springboot0/ 正文 Spring Boot使用了一个全局的配置文件application.prop ...

  5. 基于docker registry镜像安装私服docker hub

    采用docker registry镜像安装docker私服,通过https://hub.docker.com/_/registry链接搜索registry镜像 1.输入命令:docker pull r ...

  6. libOpenThreads库链接冲突错误

    最近在linux 上安装了3.7.0版本的OpenSceneGraph,而在安装之前没有完全卸载之前安装的3.6.3版本,导致在编译程序链接时出现库引用冲突,在便以后出现以下警告信息: /usr/bi ...

  7. Thread -线程的两种创建方式

    package cn.learn.thread.Thread; /* 实现Runnable必须重写run()方法,一般推荐用Runnable实现 */ public class RunnableImp ...

  8. centos nginx 设置开启启动

    1 vi /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkco ...

  9. [暑假集训Day3T1]小木棍

    经典搜索题. 考虑以下9种优化 1)按木棍长度排序,使得较大长度的木棍被较早的选出. 2)只找能够整除的木棍长度,因为不能被sum整除一定不会出整数根,自然也就不是最优解. 3)枚举木棍长度时只需从最 ...

  10. JavaScript的变量作用域

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...