【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.
For example,
Given "egg", "add", return true.
Given "foo", "bar", return false.
Given "paper", "title", return true.
Note:
You may assume both s and t have the same length.
解题思路:
首先,相同的地方必须相同这个条件可以得出一个O(n^2)的算法
class Solution:
# @param {string} s
# @param {string} t
# @return {boolean}
def isIsomorphic(self, s, t):
ls = len(s)
lt = len(t)
if ls != lt:
return False
for i in range(ls):
for j in range(ls):
if s[i] == s[j]:
if t[i] != t[j]:
return False
return True
但是很不幸超时了,于是我们用hash的方法得到另一个更快的算法:
class Solution:
# @param {string} s
# @param {string} t
# @return {boolean}
def isIsomorphic(self, s, t):
ls = len(s)
lt = len(t)
if ls != lt:
return False
dic = {}
dic2 = {}
for i in range(ls):
if s[i] not in dic.keys():
dic[s[i]] = t[i]
else:
if dic[s[i]] != t[i]:
return False
if t[i] not in dic2.keys():
dic2[t[i]] = s[i]
else:
if dic2[t[i]] != s[i]:
return False
return True
【leetcode】Isomorphic Strings的更多相关文章
- 【leetcode】Isomorphic Strings(easy)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 【Leetcode】【Easy】Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 【leetcode】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- 【leetcode】Multiply Strings(middle)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
随机推荐
- pip 与pip3
pip 3用于python3 版本 pip2 用于python2版本 pip好像可以通用 "pip2" is for Python2, "pip3″ is for Pyt ...
- jquery1.7.2的源码分析(三)$.Deferred
例子的详细讲解 Filter Resolve var filterResolve = function() { var defer = $.Deferred(), filtered = defer.t ...
- apache配置 php中没有php5apache2_4.dll
apache配置 php中没有php5apache2_4.dll 今天,在win7系统下安装Apache+PHP+MySQL时,终于体会到了版本多也不一定是好事,各种不兼容,主要是因为动态包的多少问题 ...
- Nginx 和 IIS 实现动静分离
前段时间,搞Nginx+IIS的负载均衡,想了解的朋友,可以看这篇文章:<nginx 和 IIS 实现负载均衡>,然后也就顺便研究了Nginx + IIS 实现动静分离.所以,一起总结出来 ...
- .NET Framework(.config)的配置文件架构
配置文件是标准的XML文件..NET Framework定义了一组实现配置设置的元素.本节描述计算机配置文件.应用程序配置文件和安全配置文件的配置架构.如果希望直接编辑配置文件,您需要熟悉XML.XM ...
- Git 分支管理和冲突解决
Git 分支管理和冲突解决 创建分支 git branch 没有参数,显示本地版本库中所有的本地分支名称. 当前检出分支的前面会有星号. git branch newname 在当前检出分支上新建分支 ...
- bzoj1492 斜率优化|cdq分治
#include <stdio.h> #include <bitset> #include <string.h> #include <stack> #i ...
- Spark 官方文档(5)——Spark SQL,DataFrames和Datasets 指南
Spark版本:1.6.2 概览 Spark SQL用于处理结构化数据,与Spark RDD API不同,它提供更多关于数据结构信息和计算任务运行信息的接口,Spark SQL内部使用这些额外的信息完 ...
- ASP.NET运作流程
当我们在浏览器输入域名访问服务器资源时,会向服务器发送Http请求,并经由IIS处理后,交由ASP.NET托管程序处理,进入ASP.NET管道.在IIS内部如何处理我们不需要深入去了解,在ASP.NE ...
- iOS开发 Xcode8中遇到的问题及改动
iOS开发 Xcode8中遇到的问题及改动 新版本发布总会有很多坑,也会有很多改动. 一个一个填吧... 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功能,或者苹果健康 ...