[leetcode] 205. Isomorphic Strings 解题报告

题目链接:https://leetcode.com/problems/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.


思路:hash表

代码如下:

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int map1[256] = {0};
        int map2[256] = {0};
        for(int i = 0; i< s.size(); i++)
        {
            if(map1[s[i]] == 0) map1[s[i]] = i+1;
            if(map2[t[i]] == 0) map2[t[i]] = i+1;
            if(map1[s[i]] != map2[t[i]]) return false;
        }
        return true;
    }
};

参考:https://leetcode.com/discuss/79862/simple-8ms-c-solution-o-n-time-and-o-1-space

你可能感兴趣的:(LeetCode,hash)