题目:
Given two stringss andt, determine if they are isomorphic.
Two strings are isomorphic if the characters ins can be replaced to gett.
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.
给定两个字符串s和t,确定它们是同构的。如果s中的字符可以被替换为t,那么两个字符串是同构的。在保存字符顺序的同时,必须出现替换字符的所有,没有两个字符可以映射到相同的字符,但是一个字符可以映射到自己。假设s和t都有相同的长度。
思路:
这里用到了哈希表,分别装载两个字符串,而我直接定义两个变量去装载访问,定义两个哈希表,第一个哈希表在遍历第一个字符串的同时,记录当前字符最后一次出现的位置,如果该哈希值包含了s[i]那么第二个哈希值也一定要包含t[i]并且是s[i]最后一次出现的位置也要等于t[i]最后一次出现的位置,才能成为同构。但是如果第一个哈希表中没有包含s[i],那么第二个哈希表不一定不能包含t[i]。ContainKey()方法用来检测检测数据是否存在。