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. python协程

    http://bingotree.cn/?p=63 协程与yield的介绍,轻松搞笑. http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d ...

  2. AutoMapper Getting started

    AutoMapper 是什么? 为什么要用AutoMapper? 如何使用AutoMapper? 在什么地方配置AutoMapper? 如何测试my mappings? AutoMapper 是什么? ...

  3. 怎样将myeclipse里默认编码设置成utf-8

    需要设置三个位置: [1]需要在  Preferences->general->content types->下角是文件编码,可以自己定义 [2]windows->Prefer ...

  4. java sleep() 、yield()

    1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...

  5. asp.net中获取当前url的方法

    HttpContext.Current.Request.Url.ToString() 并不可靠. 如果当前URL为 http://localhost/search.aspx?user=http://c ...

  6. js 中 toString( ) 和valueOf( )

    1.toString()方法:主要用于Array.Boolean.Date.Error.Function.Number等对象转化为字符串形式.日期类的toString()方法返回一个可读的日期和字符串 ...

  7. MVP设计模式的实现

    MVP:界面与业务逻辑分离在Winform中的应用 MVP,Model-View-Presenter的缩写. 在MSDN上,下载了一个示例,http://www.microsoft.com/china ...

  8. yii和wp做博客

    第一步,安装yii和wp: 第二步,创建protected/components/ExceptionHandler.php文件 <?php class ExceptionHandler { pu ...

  9. Java实现文件复制的四种方式

    背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制. 实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操 ...

  10. iOS 本人必装插件

    本人觉得比较好用也实用的Xcode插件记录: 1. Alcatraz   插件通过它来管理 :    https://github.com/alcatraz/Alcatraz.git 2. Cocoa ...