diff options
Diffstat (limited to 'isomorphic_strings.py')
-rw-r--r-- | isomorphic_strings.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/isomorphic_strings.py b/isomorphic_strings.py new file mode 100644 index 0000000..68178a1 --- /dev/null +++ b/isomorphic_strings.py @@ -0,0 +1,32 @@ +# 205. Isomorphic Strings + +""" +given two strings 's' and 't', determine if they are isomorphic. two strings +'s' and 't' are isomorphic if the characters in 's' can be replaced to get +'t'. all occurences of a character must be replaced with another character +while presevering the order of characters. no two characters may map to the +same character, but a character may map to itself. +""" + + +class Solution(object): + def isIsomorphic(self, s, t): + """ + :type s: str + :type t: str + :rtype: bool + """ + s1, t1 = [0 for _ in range(256)], [0 for _ in range(256)] + for i in range(len(s)): + if s1[ord(s[i])] != t1[ord(t[i])]: + return False + s1[ord(s[i])] = i + 1 + t1[ord(t[i])] = i + 1 + return True + + +if __name__ == "__main__": + obj = Solution() + print(obj.isIsomorphic("egg", "add")) + print(obj.isIsomorphic("foo", "bar")) + print(obj.isIsomorphic("paper", "title")) |