首页 > 其他 > 详细

205. 同构字符串

时间:2020-04-04 17:57:25      阅读:66      评论:0      收藏:0      [点我收藏+]
 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 };

 

205. 同构字符串

原文:https://www.cnblogs.com/yuhong1103/p/12632244.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!