File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ /* 
2+ Given two strings s and t, determine if they are isomorphic. 
3+ 
4+ Space: O(1) 
5+ Time: O(n) 
6+ */ 
7+ 
8+ bool  isIsomorphic (char  *  s , char  *  t ){
9+     int  alphabet_s [256 ]; // Alphabet of t letters to s 
10+     int  alphabet_t [256 ]; // Alphabet of s letters to t 
11+     for  (int  i = 0 ; i < 256 ; i ++ ){ // Fill alphabets with empty values 
12+         alphabet_s [i ] =  -1 ;
13+         alphabet_t [i ] =  -1 ;
14+     }
15+     int  i ; // To be able to use it outside the loop for 
16+     for  (i = 0 ; s [i ]!= '\0' ; i ++ ) {
17+         if  (alphabet_t [s [i ]]== -1  &&  alphabet_s [t [i ]]== -1 ) {
18+             alphabet_t [s [i ]] =  t [i ];
19+             alphabet_s [t [i ]] =  s [i ];
20+         } else  if  (alphabet_t [s [i ]]== -1 ) {
21+             if  (alphabet_s [t [i ]] !=  s [i ]) {
22+                 return  false;
23+             }
24+             alphabet_t [s [i ]] =  t [i ];
25+         }  else  if  (alphabet_s [t [i ]]== -1 ) {
26+             if  (alphabet_t [s [i ]] !=  t [i ]) {
27+                 return  false;
28+             }
29+             alphabet_s [t [i ]] =  s [i ];
30+         } else  if  (alphabet_t [s [i ]] !=  t [i ] ||  alphabet_s [t [i ]] !=  s [i ]) {
31+             return  false;
32+         }
33+     }
34+     return  t [i ]== '\0' ;
35+ }
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments