컴퓨터공학/알고리즘 문제 풀이

[LeetCode] 205. Isomorphic Strings

TaeGyeong Lee 2023. 2. 13. 17:44

제 3의 문자열로 표현하여 문제를 해결할 수 있습니다.

https://leetcode.com/problems/isomorphic-strings/

 

Isomorphic Strings - LeetCode

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 occurrences of a character must be replaced with another character while preserving th

leetcode.com


솔루션

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        s_count, t_count = 0,0
        s_list, t_list = [], []

        for i in range(len(s)):
            # search s
            for s_i, s_v in enumerate(s[:i+1]):
                if s_v == s[i]:
                    s_list.append(s_count)
                    break
                s_count += 1
            # search t
            for t_i, t_v in enumerate(t[:i+1]):
                if t_v == t[i]:
                    t_list.append(t_count)
                    break
                t_count += 1
        if s_list == t_list:
            return True
        else :
            return False