1+ /* Алгоритмы <3 <3
2+ TASK 0
3+ Проверьте 2 строки и определите изоморфные ли они.
4+ Две строки являются изоморфными если все их символы
5+ s, могут быть заменены t.
6+ Все символы одной строки могут быть заменены такими же символами другой строки, независимо от
7+ порядка символов.
8+ Given two strings, determine if they are isomorphic.
9+ Two strings are isomorphic if the characters in s can be replaced to get t.
10+ All occurrences of a character must be replaced with another character while preserving the order of characters.
11+ No two characters may map to the same character but a character may map to itself."
12+ arguments ["egg", "add"] => expected true
13+ arguments ["foo", "bar"] => expected false
14+ arguments ["paper", "title"] => expected true
15+ arguments ["ca", "ab"] => expected true
16+ arguments ["aa", "bb"] => expected true
17+ arguments ["aedor", "eiruw"] => expected true
18+ */
19+
20+ const solution = ( arr ) => {
21+ const firstString = arr [ 0 ] ;
22+ const secondString = arr [ 1 ] ;
23+
24+ if ( firstString . length !== secondString . length ) return false ;
25+
26+ const letterMap = { } ;
27+
28+ for ( var i = 0 ; i < firstString . length ; i ++ ) {
29+ var letterA = firstString [ i ] ,
30+ letterB = secondString [ i ] ;
31+
32+ if ( letterMap [ letterA ] === undefined ) {
33+ letterMap [ letterA ] = letterB ;
34+ } else if ( letterMap [ letterA ] !== letterB ) {
35+ return false ;
36+ }
37+ }
38+ return true ;
39+ } ;
40+
41+ console . log ( solution ( [ "egg" , "add" ] ) ) ;
42+ console . log ( solution ( [ "foo" , "bar" ] ) ) ;
43+ console . log ( solution ( [ "paper" , "title" ] ) ) ;
44+ console . log ( solution ( [ "ca" , "ab" ] ) ) ;
45+ console . log ( solution ( [ "aa" , "bb" ] ) ) ;
46+ console . log ( solution ( [ "aedor" , "eiruw" ] ) ) ;
0 commit comments