[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 ...
随机推荐
- jquery验证手机号码和固定电话号码
<pre name="code" class="javascript"> //验证手机号码或者电话号码 function checkContactN ...
- [20160731]read a file and print it on the screen
//read a file and print it on the screen import java.io.*; public class MyPrintStreamTest2{ public s ...
- Jquery Ajax处理,服务端三种页面aspx,ashx,asmx的比较
常规的Jquery Ajax 验证登录,主要有3种服务端页面相应 ,也就是 aspx,ashx,asmx即webserivice . 下面分别用3种方式来(aspx,ashx,asmx)做服务端来处理 ...
- SQL Server被锁的表以及解锁
select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys ...
- Linux之图形化shell------dialog
转自:Linux dialog详解(图形化shell) | 运维生存时间 对话 UNIX: 使用 shell 脚本创建好的图形应用程序---http://www.ibm.com/developerwo ...
- Java for LintCode 验证二叉查找树
给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值. 节点的右子树中的值要严格大于该节点的值. 左右子树也必须是二叉查找树. ...
- Struts2常用标签
Struts2常用标签总结 一 介绍 1.Struts2的作用 Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,struts2的主题.模板都提供了很好的扩展性.实现了更好的 ...
- 修改UINavigationController返回按钮颜色
系统默认颜色是蓝色的 视觉效果非常难看 在push进的ViewController中写 //修改UINavigationController的文字颜色 self.navigationControlle ...
- linux线程的实现
首先从OS设计原理上阐明三种线程:内核线程.轻量级进程.用户线程 内核线程 内核线程就是内核的分身,一个分身可以处理一件特定事情.这在处理异步事件如异步IO时特别有用.内核线程的使用是廉价的,唯一使用 ...
- cocos2d-x 第二篇 HelloWorld的流程
这篇博客主要是带领大家一起了解整个游戏的执行过程,其中涉及的一些譬如导演,场景,层之类的概念将会在后面讲解. 看main函数的区别: #import <UIKit/UIKit.h> // ...