본문 바로가기

Algorithm/Leetcode

[Top Interview 150 - Hashmap] 205. Isomorphic Strings

[Q] [Easy]

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 the order of characters. 
No two characters may map to the same character, but a character may map to itself.

 

  • 길이 다르면 애초에 아니니까 따로 빼줬다.
  • str.index() 함수 활용하여 index 들을 확인해줬다.
  • str.index() 함수는 중복되어도 첫번째 인덱스를 추출해주기 때문에 이럴때 활용하기 좋다.
# Answer

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:

        # 길이 다르면 False
        if len(s) != len(t):
            return False

        else:
            s_index = []
            t_index = []

            for i in s:
                s_index.append(s.index(i))
            
            for j in t:
                t_index.append(t.index(j))

            if s_index == t_index:
                return True