leetcode 205. Isomorphic Strings

題目描述

ASCII碼錶中的控制字符和可顯示字符共有128個,因此能夠將string中出現的字符一次轉換成數字就可比較大小。spa

即將兩個string都轉換成數字來比較大小code

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;
        
    }

};
相關文章
相關標籤/搜索