My 8ms cpp code


  • 0
    C
    class Solution {
    public:
        bool isIsomorphic(string s, string t) 
        {
            // if s[i]->t[i], then map s[i] and t[i] to the same integer;
            // if s[i] is not mapped, but t[i]!=0, then it's not a one-to-one mapping, return false;
            
            int map_s[256] = {0};
            int map_t[256] = {0};
            
            for(int i=0; i<s.size(); i++)
            {
                if(map_s[ s[i] ] && map_s[ s[i] ] == map_t[ t[i] ])
                    continue;
                    
                if(!map_s[ s[i] ] && !map_t[ t[i] ])
                {
                    map_s[ s[i] ] = i+1;
                    map_t[ t[i] ] = i+1;
                    continue;
                }
                
                return false;
            }
            
            return true;
        }
    };

  • 0
    M
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.