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<iostream> #include<algorithm> #include<map> using namespace std; bool iequal(string, string); int main() { string a = "asdfgh"; string b = "hgfhsn"; bool temp; temp = iequal(a, b); cout << temp; system("pause"); return 0; } bool iequal(string a, string b) { map<char, char>m1; map<char, char> m2; int len1 = a.size(); int len2 = b.size(); if (len1 != len2) return false; for (int i = 0; i < len1; i++) { if (m1.find(a[i]) == m1.end()) m1[a[i]] = b[i]; else { if (m1[a[i]] != b[i]) return false; } } for (int i = 0; i < len1; i++) { if (m2.find(b[i]) == m2.end()) m2[b[i]] = a[i]; else { if (m2[b[i]] != a[i]) return false; } } return true; }