【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 ...
随机推荐
- Echarts 饼图标题文字换行问题
var option = { title : { text: '数据来源', x:'center' }, tooltip : { trigger: 'item', formatter: "{ ...
- 5天揭秘js高级技术-第一天
一.基础杂记 1. document.write() <script type="text/javascript"> document.write('<h2> ...
- 在Unity中使用UGUI修改Mesh绘制几何图形
在商店看到这样一个例子,表示很有兴趣,他们说是用UGUI做的.我想,像这种可以随便变形的图形,我第一个就想到了网格变形. 做法1: 细心的朋友应该会发现,每个UGUI可见元素,都有一个‘Canvas ...
- linux创建新用户以及修改密码
1. 使用root账户创建新用户 useradd webuser 2. 修改新增的用户的密码 passwd webuser 这时候会提示你输入新的密码: 注意:不要用su webuser进入该账户修改 ...
- Python - 类与对象的方法
类与对象的方法
- PHP之compact()函数
PHP之compact()函数 compact()函数将变量转化为数组: <?php //直接调用函数 $a=1;$b=3;$c=4; var_dump($a,$b,$c); //自定义函数实现 ...
- css中的负边距
css中的负边距(negative margin)是布局中的一个常用技巧,只要运用得合理常常会有意想不到的效果.很多特殊的css布局方法都依赖于负边距,所以掌握它的用法对于前端的同学来说,那是必须的. ...
- Kali 使用ssh,安装vmware tools 和字体重叠
Kali一直是我所爱,说说遇到的问题吧 第一个:字体重叠 gnome-tweak-tool --打开工具 第二个:ssh 修改sshd_config文件,命令为: vi /etc/ssh/sshd_c ...
- ubuntu15:10 163源
sudo vim /etc/apt/sources.list deb http://mirrors.163.com/ubuntu/ wily main restricted universe mult ...
- linux shell输入重定向
经常用(隔很久)经常忘. 备忘下:http://blog.csdn.net/ithomer/article/details/9288353