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.
#include <string>
#include <vector>
using std::string;
using std::vector;
class Solution
{
public:
bool isIsomorphic(string s, string t)
{
vector<unsigned char> First(256, 0);
vector<unsigned char> Second(256, 0);
const int SIZE = s.size();
for (int Index = 0; Index < SIZE; Index++)
{
unsigned char ElementOfFirst = s[Index];
unsigned char ElementOfSecond = t[Index];
unsigned char& First2Second = First[ElementOfSecond];
unsigned char& Second2First = Second[ElementOfFirst];
if (First2Second != 0 && Second2First != 0)
{
if (First2Second != ElementOfFirst)
{
return false;
}
if (Second2First != ElementOfSecond)
{
return false;
}
continue;
}
if (First2Second == 0 && Second2First == 0)
{
First2Second = ElementOfFirst;
Second2First = ElementOfSecond;
continue;
}
return false;
}
return true;
}
};
原文地址:http://blog.csdn.net/sheng_ai/article/details/45815515