LeetCode 205 solution
205. Isomorphic Strings
#Hash_table #String
problem
Given two stringss
andt
, determine if they are isomorphic.
Two stringss
andt
are isomorphic if the characters ins
can be replaced to gett
.
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
python
from typing import List
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
res = False
if self.freq_dict(s) == self.freq_dict(t):
res = True
return res
def freq_dict(self, s: str) -> List:
res = []
appear = []
for i in s:
if i not in appear:
appear.append(i)
res.append(appear.index(i))
else:
res.append(appear.index(i))
return res
hash table을 사용하는 것이 좋을 것이다. 이 방식대로 풀면 `O(n^2)` 에 가까워지기 때문에 사실상 시간 복잡도나 공간 복잡도 측면에서 좋은 풀이법은 아니다.