题目简述:

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)的算法

  1. class Solution:
  2. # @param {string} s
  3. # @param {string} t
  4. # @return {boolean}
  5. def isIsomorphic(self, s, t):
  6. ls = len(s)
  7. lt = len(t)
  8. if ls != lt:
  9. return False
  10. for i in range(ls):
  11. for j in range(ls):
  12. if s[i] == s[j]:
  13. if t[i] != t[j]:
  14. return False
  15. return True

但是很不幸超时了,于是我们用hash的方法得到另一个更快的算法:

  1. class Solution:
  2. # @param {string} s
  3. # @param {string} t
  4. # @return {boolean}
  5. def isIsomorphic(self, s, t):
  6. ls = len(s)
  7. lt = len(t)
  8. if ls != lt:
  9. return False
  10. dic = {}
  11. dic2 = {}
  12. for i in range(ls):
  13. if s[i] not in dic.keys():
  14. dic[s[i]] = t[i]
  15. else:
  16. if dic[s[i]] != t[i]:
  17. return False
  18. if t[i] not in dic2.keys():
  19. dic2[t[i]] = s[i]
  20. else:
  21. if dic2[t[i]] != s[i]:
  22. return False
  23. 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. checkbox做全选按钮

    1.先写一个html页面,里面写一个全选按钮和几个复选框,实现下面2个要求 (1)点击全选按钮选中时,所有的复选框选中. (2)点击全选按钮取消选中时,所有复选框取消选中. <input typ ...

  2. 在Main方法中设置异常的最后一次捕捉

    在做Winfrom程序时,有时会遇到一个异常,可是这个异常不知道在什么地方发生的,程序会自动关闭,然后什么也没有了,在网上找到了一种方法,用来捕捉这种异常. 出现这种情况的原因是在程序中某些地方考虑不 ...

  3. Hash算法专题

    1.[HDU 3068]最长回文 题意:求一个字符串(len<=110000)的最长回文串 解题思路:一般解法是manacher,但是这一题用hash也是可以ac的 假设当前判断的是以i为中心偶 ...

  4. Linux快速上手

    1.Linux系统架构 内核(kernel) 内存管理(mm) Linux内存特性无论物理内存有多大,Linux 都将其充份利用,将一些程序调用过的硬盘数据读入内存,利用内存读写的高速特性来提高Lin ...

  5. Linux或Unix环境利用符号链接升级Maven

    1,解压Maven到安装目录,在解压目录同一级创建刚解压目录的符号链接,命令如下: ln -s apache-maven-3.3.9 apache-maven 2,配置环境变量,这里Maven主目录环 ...

  6. //给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和

    //给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和 # include<stdio.h> void main() { ,sum1; ]={,- ...

  7. redis实战(01)_redis安装

    早就想对redis进行实战操作了,最近看了一些视频和参考书籍,总结总结一下,redis实战内容: 实战前先对redis做一个大概的认识: 现在开始安装redis了... redis的安装下载地址 ht ...

  8. word20161217

    p-node / p 节点 package / 程序包 packet / 数据包 packet assembler/disassembler, PAD / 分组拆装器 packet header / ...

  9. canvas简介

    一.canvas简介 1.1 什么是canvas?(了解) 是HTML5提供的一种新标签 <canvas></canvas> 英 ['kænvəs] 美 ['kænvəs] 帆 ...

  10. jQuery最佳实践

    1:事件的委托处理(Event Delegation) javascript的事件模型,采用"冒泡"模式,也就是说,子元素的事件会逐级向上"冒泡",成为父元素的 ...