Isomorphic Strings

Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy

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.

/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isIsomorphic = function(s, t) {
var sp = 0;
var tp = 0; var sm = {};
var tm = {}; if (s.length != t.length) return false; var st = "";
for (var i = 0; i < s.length; i++) {
if (sm[s[i]] == void 0) {
sm[s[i]] = sp++;
}
st += sm[s[i]];
} var tt = "";
for (var i = 0; i < t.length; i++) {
if (tm[t[i]] == void 0) {
tm[t[i]] = tp++;
}
tt += tm[t[i]];
} if (st == tt) return true;
return false;
};

好像是word pattern 的姊妹题,这题是找pattern,所以比找match 要容易点。最简单的办法就是利用哈希表和一个递增的整数来把字符串归一化,如果归一化后的结果是一样的则是相同pattern 的字符串。javasscript 写起来方便但是要注意 !0 == true,所以改为判断和void 0 比较了。

[LeetCode] Isomorphic Strings的更多相关文章

  1. [LeetCode] Isomorphic Strings 同构字符串

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

  2. Python3解leetcode Isomorphic Strings

    问题描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  3. LeetCode Isomorphic Strings 对称字符串

    题意:如果两个字符串是对称的,就返回true.对称就是将串1中的同一字符都一起换掉,可以换成同串2一样的. 思路:ASCII码表哈希就行了.需要扫3次字符串,共3*n的计算量.复杂度O(n).从串左开 ...

  4. leetcode:Isomorphic Strings

    Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...

  5. [leetcode]205. Isomorphic Strings 同构字符串

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

  6. LeetCode 205. 同构字符串(Isomorphic Strings)

    205. 同构字符串 205. Isomorphic Strings

  7. 【刷题-LeetCode】205. Isomorphic Strings

    Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...

  8. 205. Isomorphic Strings - LeetCode

    Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...

  9. LeetCode_205. Isomorphic Strings

    205. Isomorphic Strings Easy Given two strings s and t, determine if they are isomorphic. Two string ...

随机推荐

  1. BestCoder Round #61 1002 Game

    Problem Description XY is playing a game:there are N pillar in a row,which numbered from 1 to n.Each ...

  2. python 内置速度最快算法(堆排)

    import random import time from heapq import heappush, heappop def heapsort(iterable): h = [] for val ...

  3. 7.2---蚂蚁相遇问题(CC150)

    public class Ants { public double antsCollision(int n) { // write code here return (1 - Math.pow(0.5 ...

  4. python中单元测试/数据库预处理的技巧

    假设文件结构: pkg/ __init__.py components/ core.py __init__.py tests/ core_test.py __init__.py python -m 你 ...

  5. php上传文件大小限制修改

    打开php.ini 1.最大上传文件大小: upload_max_filesize=2M 改成自己需要的大小 2.最大post大小: post_max_size=2M 改成自己需要的大小,第二个一般比 ...

  6. etcd相关资料

    <1>etcd:从应用场景到实现原理的全方位解读 http://www.infoq.com/cn/articles/etcd-interpretation-application-scen ...

  7. eos超时 锁表问题 网友办法

    select * from v$locked_object; SELECT sid, serial#, username, osuser FROM v$session where sid = 45; ...

  8. 基于MATLAB的离散小波变换

    申明,本文非笔者原创,原文转载自:  基于Matlab的离散小波变换         http://blog.sina.com.cn/s/blog_725866260100ryh3.html 简介 在 ...

  9. js隐藏div和class

    <style type="text/css"> //div用点//class# .footer {  display:none;  } #footer {  displ ...

  10. Wince下sqlce数据库开发(一)

    对于Wince下的sqlce数据库虽然很多人在用,但在我查找资料时,却发现资料是多么的匮乏,在此对自己这几天的了解做个简单介绍,希望对大家能有所帮助! 本文的最后附有所使用到的sqlce在wince下 ...