Isomorphic Strings

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

For example,
Given "egg", "add", return true.

Given "foo", "bar", return false.

Given "paper", "title", return true.

Note:
You may assume both s and t have the same length.


class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int len1=s.length();
        int len2=t.length();
        if(len1!=len2){
            return false;
        }
        char charMap[256];
        char charMap1[256];
        memset(charMap, 0, sizeof(char)*256);
        memset(charMap1, 0, sizeof(char)*256);
        for(int i=0; i<len1; i++){
            if(charMap[s[i]]==0){
                charMap[s[i]] = t[i];
            }else if(charMap[s[i]]!=t[i]){
                return false;
            }
            if(charMap1[t[i]]==0){
                charMap1[t[i]] = s[i];
            }else if(charMap1[t[i]]!=s[i]){
                return false;
            }
        }
        return true;
    }
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章