관리 메뉴

솜씨좋은장씨

[leetCode] 205. Isomorphic Strings (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 205. Isomorphic Strings (Python)

솜씨좋은장씨 2020. 8. 3. 22:52
728x90
반응형

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.

 

Example 1:

Input: s = "egg", t = "add"
Output: true

Example 2:

Input: s = "foo", t = "bar"
Output: false

Example 3:

Input: s = "paper", t = "title"
Output: true

Note:
You may assume both and have the same length.

 

Solution

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        check_dict = {}
        check_dict_2 = {}
        answer = True
        
        for s_c, t_c in zip(s, t):
            if s_c not in check_dict.keys():
                check_dict[s_c] = t_c
            else:
                if check_dict[s_c] != t_c:
                    answer = False
                    break
        
        if answer == True:
            for s_c, t_c in zip(s, t):
                if t_c not in check_dict_2.keys():
                    check_dict_2[t_c] = s_c
                else:
                    if check_dict_2[t_c] != s_c:
                        answer = False
                        break
                    
        return answer

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments