#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.

Example 1:

Input: s = "egg", t = "add"
Output: true

Example 2:

Input: s = "foo", t = "bar"
Output: false

Example 3:

Input: s = "paper", t = "title"
Output: true

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

代码:

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        map<char, char> mp1, mp2;
        int ls = s.size(), lt = t.size();
        if(ls != lt) return false;
        
        for(int i = 0; i < ls; i ++) {
            if(mp1.find(s[i]) != mp1.end() && mp1[s[i]] != t[i]) return false;
            if(mp2.find(t[i]) != mp2.end() && mp2[t[i]] != s[i]) return false;
            mp1[s[i]] = t[i]; mp2[t[i]] = s[i];
        }
        return true;
    }
};

  看到题目里面的映射想到了这么写 今天好像很多 map

posted @ 2018-11-30 15:32  丧心病狂工科女  阅读(131)  评论(0编辑  收藏  举报