This is a thing of mapping. On each occurrence, record the mapping. If one char maps to two chars, False; or two chars map to the same char, False.
Solution:
# T:O(n) S:O(1) At most 26 mappings class Solution: # @param {string} s # @param {string} t # @return {boolean} def isIsomorphic(self, s, t): length, dict = len(s), {} for i in xrange(length): if s[i] not in dict: if t[i] in dict.values(): return False dict[s[i]] = t[i] else: if dict[s[i]] != t[i]: return False return TrueRun Time: 64 ms
No comments:
Post a Comment