【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 ...
随机推荐
- 如何解决Maven和SBT下载Jar包太慢
国内:如何解决Maven和SBT下载Jar包太慢 Maven 远程仓库 <mirror> <id>ui</id> <mirrorOf>central&l ...
- asp.net反向代理
https://www.codeproject.com/Articles/18490/Reverse-Proxy-in-C-NET-v https://www.codeproject.com/Arti ...
- ThinkPHP Where 条件中使用表达式
本文转自:这里 Where 条件表达式格式为: $map['字段名'] = array('表达式', '操作条件'); 其中 $map 是一个普通的数组变量,可以根据自己需求而命名.上述格式中的表达式 ...
- vmware workstation安装 Mosrosoft Runtime DLL安装程序未能完成安装
不要点确定.开始菜单运行输入'%temp%',在弹出的窗体中找到一个文件名中含'{132E3257-14F1-411A-BC6C-0CA32D3A9BC6}~setup'(不一定一样,反正就是第一行的 ...
- 各种Js封装
获取ClassName元素 function getClass(classname,id){ if(document.getElementsByClassName){ if(id){ return $ ...
- 如何查看/统计当前AD域控制器的活动用户?
最近公司想知道某台AD域控制器上当前连接了多少活动用户? 此前个人只知道以下不是非常完善且统计起来比较麻烦的方法: 方法1:查看共享会话数.(不完全准确) 方法2:查看当前的DNS记录.(这种方法统计 ...
- 【学习笔记】C语言之词法规则
一.字符 标准并没有规定C环境必须使用特定的字符集,但是它规定了字符集必须包含英语所有的大小写字母,数字0到9,以及下面的字符: ! # % ^ & * ( ) _ – + = / . ? ...
- Linux vmstat使用
Vmstat命令的简单使用 Vmstat命令是Linux/unix常用的系统监控工具,可以方便的查看CPU.内存.swap分区.IO读写等情况. Vmstat常用的参数主要有两个:1.采集的时间间隔 ...
- 码云以及git使用
码云的使用方法以及git的连用 创建公钥的方法 打开码云,点击个人资料---->SSH公钥---->点击怎样生成公钥 SSH Keys ssh keys可以让你在你的电脑和Git@OSC知 ...
- 释放修改OS X 10.11系统文件权限【转】
序言:有时要替换相关的(系统目录下的)文件以完成软件的破解,但在 OS X 10.11 系统图形界面下,Root(系统超级用户)已‘转变’为 Administrator(管理员用户),选择系统文件夹( ...