题目简述:

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

  1. 【leetcode】Isomorphic Strings(easy)

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

  2. 【Leetcode】【Easy】Isomorphic Strings

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

  3. 【leetcode】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  4. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  6. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  7. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

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

  9. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

随机推荐

  1. cygwin 安装完后不能进入think问题,网上99%都是错误的

    正确的做法是首次进入的时候,显示的是哪个用户名就修改哪个用户名 比如我的电脑是 默认显示的是think 那么就去修改think 为 root 并把unused 后的2个数字改成0 然后在home下新建 ...

  2. unity游戏开发新手-----2017年展望

    0.希望三月份中旬之前找一份游戏开发的工作,必须转正; 1.希望存款3-4万; 2.今年年底结婚; 3.锻炼身体,体重保持在115斤左右,有胸肌和腹肌;(结婚之前实现) 4.技术方面: 熟练掌握C#语 ...

  3. Spring MVC学习笔记——返回JSON对象

    1.想要GET请求返回JSON对象,首先需要导入jackson-all-1.9.4.jar包 2.在控制器中添加不同的show()方法 //show()方法返回JSON对象 @RequestMappi ...

  4. eclipse使用sublime配色(转)

    转自 Eclipse设置类似Sublime Text 编辑区皮肤,风格,颜色 1.首先打开eclipse 2.help -> Install New SoftWare  3.点击 Add 在Na ...

  5. express之sendFile

    module.exports = function(req, res, opt) { var applyNo = req.query.applyNo; console.log("applyN ...

  6. CSS的选择器

    <div id="demo"> <div class="inner"> <p><a href="#" ...

  7. asp.net core输出中文乱码的问题

    摘要 在学习asp.net core的时候,尝试在控制台,或者页面上输出中文,会出现乱码的问题. 问题重现 新建控制台和站点 public class Program { public static ...

  8. Java程序员笔试、面试题目

    1. 面向对象编程的三大特性是什么,请简要阐述 2. String 和StringBuffer的区别 3. 说出ArrayList,Vector, LinkedList的存储性能和特性 4. Coll ...

  9. xml中处理大于小与符号

    原符号   <    <=    >    >=     &      '       " 替换符号 < <= > >= & ...

  10. JDK,JRE,JVM,三者的区别于联系?

    万事开头难,从基础抓起! 下载JDK官网:http://www.oracle.com/technetwork/java/javase/downloads/index.html JDK:Java Dev ...