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.
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.
Hash Table,由于No two characters may map to the same character but a character may map to itself.
判断完s->t和t->s即可。
1 class Solution { 2 public: 3 bool isIsomorphic(string s, string t) { 4 unordered_map<char,char> showed; 5 for(int i=0;i<s.length();i++) 6 { 7 if(showed.find(s[i])!=showed.end()) 8 { 9 if(showed[s[i]]!=t[i]) 10 { 11 return false; 12 } 13 } 14 else 15 { 16 showed[s[i]]=t[i]; 17 } 18 } 19 showed.clear(); 20 for(int i=0;i<s.length();i++) 21 { 22 if(showed.find(t[i])!=showed.end()) 23 { 24 if(showed[t[i]]!=s[i]) 25 { 26 return false; 27 } 28 } 29 else 30 { 31 showed[t[i]]=s[i]; 32 } 33 } 34 return true; 35 } 36 };