Skip to content

Commit 2a82135

Browse files
committed
Isomorphic Strings
1 parent cd9a33e commit 2a82135

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Isomorphic Strings.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*Given two strings s and t, determine if they are isomorphic.
2+
3+
Two strings are isomorphic if the characters in s can be replaced to get t.
4+
5+
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.
6+
7+
For example,
8+
Given "egg", "add", return true.
9+
10+
Given "foo", "bar", return false.
11+
12+
Given "paper", "title", return true.
13+
14+
Note:
15+
You may assume both s and t have the same length.*/
16+
17+
/**
18+
* @param {string} s
19+
* @param {string} t
20+
* @return {boolean}
21+
*/
22+
var isIsomorphic = function(s, t) {
23+
sdict = {}
24+
tdict = {}
25+
for(i=0; i<s.length; i++){
26+
if(s[i] in sdict){
27+
if(!t[i] in tdict || sdict[s[i]] != tdict[t[i]])
28+
return false
29+
}
30+
else{
31+
if(t[i] in tdict)
32+
return false
33+
sdict[s[i]] = i
34+
tdict[t[i]] = i
35+
}
36+
}
37+
return true
38+
};

0 commit comments

Comments
 (0)