diff --git a/.plan b/.plan new file mode 100644 index 0000000..513d90c --- /dev/null +++ b/.plan @@ -0,0 +1,5 @@ +Sep 1, 2024 + +complete top interview 150 + +* solve "205. Isomorphic Strings" diff --git a/src/isomorphic_strings.py b/src/isomorphic_strings.py new file mode 100644 index 0000000..a8dda24 --- /dev/null +++ b/src/isomorphic_strings.py @@ -0,0 +1,16 @@ +class Solution: + def isIsomorphic(self, s: str, t: str) -> bool: + taken: set[str] = set() + translation: dict[str, str] = {} + + for a, b in zip(s, t): + if a not in translation: + if b in taken: + return False + + translation[a] = b + taken.add(b) + elif translation[a] != b: + return False + + return True diff --git a/tests/test_isomorphic_strings.py b/tests/test_isomorphic_strings.py new file mode 100644 index 0000000..c407a5f --- /dev/null +++ b/tests/test_isomorphic_strings.py @@ -0,0 +1,16 @@ +import pytest + +from src.isomorphic_strings import Solution + + +@pytest.mark.parametrize( + "expected,s,t", + ( + (True, "egg", "add"), + (False, "foo", "bar"), + (True, "paper", "title"), + (False, "badc", "baba"), + ), +) +def test_solution(expected, s, t): + assert expected is Solution().isIsomorphic(s, t)