#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 s and t 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

浙公网安备 33010602011771号