From bbac8cfb8a39a71f8bed030da16ebc2e15e2a42d Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Sun, 1 Sep 2024 22:41:54 +0200 Subject: [PATCH] Solve "205. Isomorphic Strings" --- .plan | 5 +++++ src/isomorphic_strings.py | 16 ++++++++++++++++ tests/test_isomorphic_strings.py | 16 ++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 .plan create mode 100644 src/isomorphic_strings.py create mode 100644 tests/test_isomorphic_strings.py 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)