4
4
* Two strings s and t are isomorphic if the characters in s can be replaced to get t.
5
5
* All occurrences of a character must be replaced with another character while preserving
6
6
* the order of characters. No two characters may map to the same character, but a
7
- * character may map to itself.
7
+ * character may map to itself.
8
8
* @param {string } s - renamed to a
9
9
* @param {string } t - renamed to b
10
10
* @return {boolean }
11
11
*/
12
12
var isIsomorphic = function ( a , b ) {
13
13
if ( a . length !== b . length ) return false ;
14
14
15
- let aX = "" , bX = "" , hashForA = [ ] , hashForB = [ ] , aCounter = 0 , bCounter = 0 ;
15
+ let aX = "" ,
16
+ bX = "" ,
17
+ hashForA = [ ] ,
18
+ hashForB = [ ] ,
19
+ aCounter = 0 ,
20
+ bCounter = 0 ;
16
21
for ( let i = 0 ; i < a . length ; i ++ ) {
17
- let charA = a [ i ] , charB = b [ i ] ;
22
+ let charA = a [ i ] ,
23
+ charB = b [ i ] ;
18
24
19
25
if ( hashForA [ charA ] == undefined ) {
20
26
hashForA [ charA ] = aCounter ;
@@ -36,16 +42,18 @@ var isIsomorphic = function (a, b) {
36
42
var isIsomorphic = ( a , b ) => {
37
43
if ( a . length !== b . length ) return false ;
38
44
39
- let a2b = [ ] , b2a = [ ] ;
45
+ let a2b = [ ] ,
46
+ b2a = [ ] ;
40
47
for ( let i = 0 ; i < a . length ; i ++ ) {
41
- let aChar = a [ i ] , bChar = b [ i ] ;
42
- if (
43
- ( a2b [ aChar ] || b2a [ bChar ] )
44
- &&
45
- ( a2b [ aChar ] !== bChar || b2a [ bChar ] !== aChar )
46
- ) return false ;
47
- if ( ! a2b [ aChar ] ) a2b [ aChar ] = bChar ;
48
- if ( ! b2a [ bChar ] ) b2a [ bChar ] = aChar ;
48
+ let aChar = a [ i ] ,
49
+ bChar = b [ i ] ;
50
+ if (
51
+ ( a2b [ aChar ] || b2a [ bChar ] ) &&
52
+ ( a2b [ aChar ] !== bChar || b2a [ bChar ] !== aChar )
53
+ )
54
+ return false ;
55
+ if ( ! a2b [ aChar ] ) a2b [ aChar ] = bChar ;
56
+ if ( ! b2a [ bChar ] ) b2a [ bChar ] = aChar ;
49
57
}
50
58
return true ;
51
- } ;
59
+ } ;
0 commit comments