ReaL simple, 8-line 8-ms c++


  • 2
    M
    class Solution {
    public:
        bool check(string s, string t) {
            int dict[256];
            fill_n(dict, 256, 0);
            for (int i=0; i<s.length(); i++)
                if (dict[s[i]]==0) dict[s[i]]=t[i];
                else if (dict[s[i]] != t[i])
                    return false;
            return true;
        }
    
        bool isIsomorphic(string s, string t) {
            if (check(s, t) && check(t,s)) return 1;
            else return 0;
        }
    };

  • 0
    N
    bool isIsomorphic(string s, string t) {
        int NO_CHAR = 256;
        int map[NO_CHAR];
        
        int map1[NO_CHAR];
        if(s.size() != t.size()) return false;
        if(s.size() < 1) return true;
        memset(map,-1, sizeof(map));
        memset(map1,-1, sizeof(map1));
        
        for(int i=0; i < s.size(); i++)
            if(map[s[i]] == -1 ){
                 if(map1[t[i]] == -1){
                 map[s[i]] = (int)t[i];
                 map1[t[i]] = 1;
                }
                else
                 return false;
            }else{
                if(map[s[i]] != -1){
                    if(map[s[i]] != (int)t[i]) return false;
                }
            }
       
               return true;
        }

Log in to reply
 

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