leetcode 205. Isomorphic Strings

题目描述

ASCII码表中的控制字符和可显示字符共有128个,所以可以将string中出现的字符一次转换成数字就可比较大小。

即将两个string都转换成数字来比较大小

class Solution {
public:
    string replace(string s){
        char tmp[128] = {0};
        char ch = '0';
        for(int i = 0; i < s.length(); i++){
            if(tmp[s[i]] == 0){
                tmp[s[i]] = ++ch;
            }
            s[i] = tmp[s[i]];
        }
        return s;
    }
    
    bool isIsomorphic(string s, string t) {
        if(s.length() != t.length())
            return false;
        if(replace(s) == replace(t))
            return true;
        else
            return false;
        
    }

};

 

posted @ 2017-06-02 13:38  StrongYaYa  阅读(385)  评论(0编辑  收藏  举报