#205 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.
最開始我是这样想的,将两个同构不同型的字符串按规则变为同构同型的字符串。比較转换后的字符串是否等。
如paper 'p'变为1,‘a’变为2,‘e’变为3 ‘r’变为4.则字符串变为 12134 title 类似变为12134。相等说明同构。
还有一种思路是分别遍历两个字符串,利用hash表中的s[i]位置存储t[i]中的字符,当下一次s字符串中再次出现s[j] ==s[i] 时,对于 t[j] 位置上的字符应该和先前的字符 t[i] 同样。
//0ms bool isIsomorphic(char* s, char* t) { int hash[128] = {0}; int i; for( i = 0; s[i] != '