algorithm/leetcode
Isomorphic Strings [LeetCode]
hoonzii
2021. 7. 12. 20:35
반응형
문제 풀이 정리
문제
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.
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
Constraints:
- 1 <= s.length <= 5 * 104
- t.length == s.length
- s and t consist of any valid ascii character.
코드
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
s_set = set([s_str for s_str in s])
t_set = set([t_str for t_str in t])
if len(s_set) != len(t_set):
return False
else:
str_dict = dict()
for a, b in zip(s,t):
if a not in str_dict:
str_dict[a] = b
else:
if str_dict[a] != b:
return False
return True
문제 넋두리
영어로 문제 보니까 더 이해를 못하는 기분...아니 기분이 아니라 사실이 그렇다 하하
하지만 아직 쉬운 문제가 잔뜩이라 그래도 다행쓰...
반응형