205. 同构字符串

 1 class Solution 
 2 {
 3 public:
 4     bool isIsomorphic(string s, string t) 
 5     {
 6         unordered_map<char,char> hash;
 7         if(s.size() != t.size()) return false;//长度不等,直接返回false
 8         int n = s.size();
 9         for(int i = 0;i < n;i ++)
10         {
11             //如果s[i]出现,且对应的哈希值与t[i]不等,直接返回false
12             if(hash.count(s[i]) && hash[s[i]] != t[i]) return false;
13            
14             string str = t.substr(0,i);
15             //如果s[i]没有出现,且它对应的哈希值已经出现,则返回false
16             if(!hash.count(s[i]) && find(str.begin(),str.end(),t[i]) != str.end()) return false;
17             hash[s[i]] = t[i];
18         }
19         return true;
20     }
21 };

 

posted @ 2020-04-04 16:06  Jinxiaobo0509  阅读(146)  评论(0)    收藏  举报