From e1eeb330a40611367231266e7f9fe35b79676910 Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 25 Apr 2026 04:38:57 -0600 Subject: [PATCH] adding updates --- .../ex_142_edit_distance.py | 15 +++++++++ .../ex_142_edit_distance.ts | 18 +++++++++++ .../test_142_edit_distance_round_22.py | 32 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_142_edit_distance.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_142_edit_distance.ts create mode 100644 tests/test_150_questions_round_22/test_142_edit_distance_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_142_edit_distance.py b/src/my_project/interviews/top_150_questions_round_22/ex_142_edit_distance.py new file mode 100644 index 00000000..570c304f --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_142_edit_distance.py @@ -0,0 +1,15 @@ +class Solution: + def minDistance(self, word1: str, word2: str) -> int: + m, n = len(word1), len(word2) + dp = list(range(n + 1)) + for i in range(1, m + 1): + prev = dp[0] + dp[0] = i + for j in range(1, n + 1): + temp = dp[j] + if word1[i - 1] == word2[j - 1]: + dp[j] = prev + else: + dp[j] = 1 + min(prev, dp[j], dp[j - 1]) + prev = temp + return dp[n] diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_142_edit_distance.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_142_edit_distance.ts new file mode 100644 index 00000000..3367d4f6 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_142_edit_distance.ts @@ -0,0 +1,18 @@ +function minDistance(word1: string, word2: string): number { + const m = word1.length, n = word2.length; + const dp: number[] = Array.from({ length: n + 1 }, (_, i) => i); + for (let i = 1; i <= m; i++) { + let prev = dp[0]; + dp[0] = i; + for (let j = 1; j <= n; j++) { + const temp = dp[j]; + if (word1[i - 1] === word2[j - 1]) { + dp[j] = prev; + } else { + dp[j] = 1 + Math.min(prev, dp[j], dp[j - 1]); + } + prev = temp; + } + } + return dp[n]; +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_142_edit_distance_round_22.py b/tests/test_150_questions_round_22/test_142_edit_distance_round_22.py new file mode 100644 index 00000000..7251c029 --- /dev/null +++ b/tests/test_150_questions_round_22/test_142_edit_distance_round_22.py @@ -0,0 +1,32 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_142_edit_distance import Solution + + +class EditDistanceTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example1(self): + self.assertEqual(self.solution.minDistance("horse", "ros"), 3) + + def test_example2(self): + self.assertEqual(self.solution.minDistance("intention", "execution"), 5) + + def test_empty_word1(self): + self.assertEqual(self.solution.minDistance("", "abc"), 3) + + def test_empty_word2(self): + self.assertEqual(self.solution.minDistance("abc", ""), 3) + + def test_both_empty(self): + self.assertEqual(self.solution.minDistance("", ""), 0) + + def test_same_words(self): + self.assertEqual(self.solution.minDistance("abc", "abc"), 0) + + def test_single_insert(self): + self.assertEqual(self.solution.minDistance("a", "ab"), 1) + + def test_single_replace(self): + self.assertEqual(self.solution.minDistance("a", "b"), 1) \ No newline at end of file