Problem:
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.
思路:
Solution (C++):
bool isIsomorphic(string s, string t) {
int n = s.length();
if (n == 0) return true;
vector<int> v1(256, 0), v2(256, 0);
for (int i = 0; i < n; ++i) {
if (v1[s[i]] != v2[t[i]]) return false;
v1[s[i]] = i+1;
v2[t[i]] = i+1;
}
return true;
}
性能:
Runtime: 4 ms Memory Usage: 7.2 MB
思路:
Solution (C++):
性能:
Runtime: ms Memory Usage: MB