Skip to content

Commit fb3f069

Browse files
committed
D. J.:
- Added leetcode problem and solution for 205 Isomorphic Strings
1 parent c0fcd1c commit fb3f069

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
1515
- [125 Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/)
1616
- [169 Majority Element](https://leetcode.com/problems/majority-element/description/)
1717
- [189 Rotate Array](https://leetcode.com/problems/rotate-array/description/)
18+
- [205 Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/description/)
1819
- [383 Ransom Note](https://leetcode.com/problems/ransom-note/description/)
1920
- [392 Is Subsequence](https://leetcode.com/problems/is-subsequence/description/)
2021

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def isIsomorphic(self, s: str, t: str) -> bool:
3+
"""
4+
Given two strings s and t, determine if they are isomorphic.
5+
6+
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
7+
8+
All occurrences of a character must be replaced with another character while preserving the order of characters.
9+
No two characters may map to the same character, but a character may map to itself.
10+
"""
11+
if len(s) != len(t):
12+
return False
13+
policy, reverse_policy = {}, {}
14+
for s_, t_ in zip(s, t):
15+
if s_ in policy and policy[s_] != t_:
16+
return False
17+
if t_ in reverse_policy and reverse_policy[t_] != s_:
18+
return False
19+
policy[s_] = t_
20+
reverse_policy[t_] = s_
21+
return True

tests/test_205_isomorphic_strings.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._205_isomorphic_strings import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["s", "t", "expected"],
8+
argvalues=[("egg", "add", True), ("foo", "bar", False), ("paper", "title", True)],
9+
)
10+
def test_func(s: str, t: str, expected: bool):
11+
is_isomorphic = Solution().isIsomorphic(s, t)
12+
assert is_isomorphic == expected

0 commit comments

Comments
 (0)