[LeetCode] Isomorphic Strings
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.
/**
* @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的更多相关文章
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Python3解leetcode Isomorphic Strings
问题描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
- LeetCode Isomorphic Strings 对称字符串
题意:如果两个字符串是对称的,就返回true.对称就是将串1中的同一字符都一起换掉,可以换成同串2一样的. 思路:ASCII码表哈希就行了.需要扫3次字符串,共3*n的计算量.复杂度O(n).从串左开 ...
- leetcode:Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- 【刷题-LeetCode】205. Isomorphic Strings
Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...
- 205. Isomorphic Strings - LeetCode
Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...
- LeetCode_205. Isomorphic Strings
205. Isomorphic Strings Easy Given two strings s and t, determine if they are isomorphic. Two string ...
随机推荐
- 转:bwa的使用方法
bwa的使用需要两中输入文件: Reference genome data(fasta格式 .fa, .fasta, .fna) Short reads data (fastaq格式 .f ...
- ubuntu下文件内容查找命令
Linux查找文件内容的常用命令方法. 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符串" 文件名 例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件 ...
- JNative library not loaded, sorry ! win7 64位系统
java调用动态链接库时,使用myeclipse或者其他IDE工具时,针对于web程序,会报这样的错误: java.lang.IllegalStateException: JNative librar ...
- SSRS报表参数设置
一.日期时间类型的参数注意事项: 关于数据类型的选择:(只有数据类型设置为日期/时间格式,在查询的时候才会显示日期控件,提示信息一般改成汉字) 指定默认值:指定开始日期为前10天,
- C#的is和as操作符来进行强制类型转换&&值类型的拆箱、装箱
if(o is Employee) { Employee e=(Employee)o; //在if语句剩余的部分中使用e; } Employee e=o as Employee; if(e!=null ...
- 《Head First Servlet JSP》web服务器,容器,servlet的职责
(一)web服务器,容器,servlet的职责 (二)J2EE服务器与web容器
- 推荐一篇 OAuth 2.0 必读文章
http://www.cnblogs.com/artech/p/oauth-03.html 共计有3篇相关内容,请仔细阅读! 再说一下我用box api 开发时的问题,在 box 程序登记页面: 对于 ...
- 【STL】 set集合容器常用用法
set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值:另外,还 ...
- linux下QT Creator常见错误及解决办法
最近因为在做一个关于linux下计算机取证的小项目,需要写一个图形界面,所以想到了用QT来写,选用了linux下的集成开发环境QT Creator5.5.1,但刚刚安装好,竟然连一个"hel ...
- HTML超链接
打开网页在 想要查看的位置右键单击 审查元素 则可以查看代码 点击图片右键单独打开 则可以查看图片位置 一.超链接 a标签 <a href="地址"> ...