Leetcode 205 Isomorphic Strings 字符串处理

判断两个字符串是否同构

hs,ht就是每个字符出现的顺序 

"egg" 与"add"的数字都是122

"foo"是122, 而"bar"是123

 1 class Solution {
 2 public:
 3     bool isIsomorphic(string s, string t) {
 4         int hs[260] = {0};
 5         int ht[260] = {0};
 6         if (s.size() != t.size()) return false;
 7         int o = 0;
 8         for (int i =0;i<s.size(); ++i)
 9         {
10             if(hs[s[i]] == 0)  hs[s[i]] = ++o;
11         }
12         o = 0;
13         for (int i =0;i<t.size(); ++i)
14         {
15             if(ht[t[i]] == 0)  ht[t[i]] = ++o;
16         }
17         for (int i = 0;i<s.size(); ++i){
18             if(hs[s[i]] != ht[t[i]])  return false;
19         }
20         return true;
21     }
22 };

 

posted @ 2016-03-13 19:33  Breeze0806  阅读(154)  评论(0编辑  收藏  举报