From 767b979f633d5aac85a2872a3dda22298ff178f5 Mon Sep 17 00:00:00 2001 From: itaim18 Date: Sun, 9 Feb 2025 17:16:02 +0200 Subject: [PATCH 1/2] Add Python solution for "Greatest Common Divisor of Strings" --- README.md | 2 +- python/1071-greatest-common-divisor-of-strings.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 python/1071-greatest-common-divisor-of-strings.py diff --git a/README.md b/README.md index 621435471..2a3329f8b 100644 --- a/README.md +++ b/README.md @@ -497,7 +497,7 @@ If you would like to have collaborator permissions on the repo to merge your own Problem | articles | C | C++ | C# | Dart | GO | hints | Java | JS | Kotlin | Python | Ruby | Rust | Scala | Swift | TS ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- [0168 - Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/) |
|
|
|
[✔️](csharp%2F0168-excel-sheet-column-title.cs)
|
|
|
|
[✔️](java%2F0168-excel-sheet-column-title.java)
|
|
[✔️](kotlin%2F0168-excel-sheet-column-title.kt)
|
[✔️](python%2F0168-excel-sheet-column-title.py)
|
|
|
|
|
-[1071 - Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/) |
|
|
[✔️](cpp%2F1071-greatest-common-divisor-of-strings.cpp)
|
|
|
[✔️](go%2F1071-greatest-common-divisor-of-strings.go)
|
|
[✔️](java%2F1071-greatest-common-divisor-of-strings.java)
|
[✔️](javascript%2F1071-greatest-common-divisor-of-strings.js)
|
[✔️](kotlin%2F1071-greatest-common-divisor-of-strings.kt)
|
|
|
[✔️](rust%2F1071-greatest-common-divisor-of-strings.rs)
|
|
|
[✔️](typescript%2F1071-greatest-common-divisor-of-strings.ts)
+[1071 - Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/) |
|
|
[✔️](cpp%2F1071-greatest-common-divisor-of-strings.cpp)
|
|
|
[✔️](go%2F1071-greatest-common-divisor-of-strings.go)
|
|
[✔️](java%2F1071-greatest-common-divisor-of-strings.java)
|
[✔️](javascript%2F1071-greatest-common-divisor-of-strings.js)
|
[✔️](kotlin%2F1071-greatest-common-divisor-of-strings.kt)
|
[✔️](python%2F1071-greatest-common-divisor-of-strings.py)
|
|
[✔️](rust%2F1071-greatest-common-divisor-of-strings.rs)
|
|
|
[✔️](typescript%2F1071-greatest-common-divisor-of-strings.ts)
[1523 - Count Odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/) |
|
|
|
|
|
|
|
|
|
[✔️](kotlin%2F1523-count-odd-numbers-in-an-interval-range.kt)
|
[✔️](python%2F1523-count-odd-numbers-in-an-interval-range.py)
|
|
|
|
|
[1572 - Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/) |
|
|
|
|
|
|
|
[✔️](java%2F1572-matrix-diagonal-sum.java)
|
[✔️](javascript%2F1572-matrix-diagonal-sum.js)
|
[✔️](kotlin%2F1572-matrix-diagonal-sum.kt)
|
[✔️](python%2F1572-matrix-diagonal-sum.py)
|
|
|
|
|
[0149 - Maximum Points on a Line](https://leetcode.com/problems/max-points-on-a-line/) |
|
|
[✔️](cpp%2F0149-max-points-on-a-line.cpp)
|
|
|
|
|
[✔️](java%2F0149-max-points-on-a-line.java)
|
[✔️](javascript%2F0149-max-points-on-a-line.js)
|
[✔️](kotlin%2F0149-max-points-on-a-line.kt)
|
[✔️](python%2F0149-max-points-on-a-line.py)
|
|
[✔️](rust%2F0149-max-points-on-a-line.rs)
|
|
|
[✔️](typescript%2F0149-max-points-on-a-line.ts)
diff --git a/python/1071-greatest-common-divisor-of-strings.py b/python/1071-greatest-common-divisor-of-strings.py new file mode 100644 index 000000000..033b1a223 --- /dev/null +++ b/python/1071-greatest-common-divisor-of-strings.py @@ -0,0 +1,15 @@ +class Solution: + def gcdOfStrings(self, str1: str, str2: str) -> str: + len1, len2 = len(str1), len(str2) + + def valid(k): + if len1 % k or len2 % k: + return False + n1, n2 = len1 // k, len2 // k + base = str1[:k] + return str1 == n1 * base and str2 == n2 * base + + for i in range(min(len1, len2), 0, -1): + if valid(i): + return str1[:i] + return "" \ No newline at end of file From cc89ee7d581f9da7ef88c54b658a7bb01706cf8e Mon Sep 17 00:00:00 2001 From: itaim18 Date: Sun, 9 Feb 2025 17:18:04 +0200 Subject: [PATCH 2/2] Refactor longest common subsequence solution by removing redundant implementations and optimizing the dynamic programming approach --- kotlin/1143-longest-common-subsequence.kt | 77 +---------------------- 1 file changed, 1 insertion(+), 76 deletions(-) diff --git a/kotlin/1143-longest-common-subsequence.kt b/kotlin/1143-longest-common-subsequence.kt index 55dc1f154..ea744645e 100644 --- a/kotlin/1143-longest-common-subsequence.kt +++ b/kotlin/1143-longest-common-subsequence.kt @@ -20,79 +20,4 @@ class Solution { } return dp[M][N] } -} - -/* - * Different solutions - */ - - // Recursion + Memoization, Time Complexity of O(n * m) and space complexity of O(n * m) -class Solution { - fun longestCommonSubsequence(t1: String, t2: String): Int { - val n = t1.length - val m = t2.length - val dp = Array (n) { IntArray (m) { -1 } } - - fun dfs(i: Int, j: Int): Int { - if (i == n || j == m) return 0 - if (dp[i][j] != -1) return dp[i][j] - - if (t1[i] == t2[j]) - dp[i][j] = 1 + dfs(i + 1, j + 1) - else - dp[i][j] = maxOf(dfs(i + 1, j), dfs(i, j + 1)) - - return dp[i][j] - } - - return dfs(0, 0) - } -} - -// Top down DP, Time Complexity of O(n * m) and space complexity of O(n * m) -class Solution { - fun longestCommonSubsequence(t1: String, t2: String): Int { - val n = t1.length - val m = t2.length - val dp = Array (n + 1) { IntArray (m + 1) } - - for (i in n - 1 downTo 0) { - for (j in m - 1 downTo 0) { - if (t1[i] == t2[j]) - dp[i][j] = 1 + dp[i + 1][j + 1] - else - dp[i][j] = maxOf(dp[i + 1][j], dp[i][j + 1]) - } - } - - return dp[0][0] - } -} - -// Optimized DP (Works both for both Top-down and Bottom-up, but here we use bottom-up approach) -// Time Complexity of O(n * m) and space complexity of O(maxOf(n, m)) -class Solution { - fun longestCommonSubsequence(t1: String, t2: String): Int { - val m = t1.length - val n = t2.length - if (m < n) return longestCommonSubsequence(t2, t1) - - var dp = IntArray (n + 1) - - for (i in m downTo 0) { - var newDp = IntArray (n + 1) - for (j in n downTo 0) { - if (i == m || j == n) { - newDp[j] = 0 - } else if (t1[i] == t2[j]) { - newDp[j] = 1 + dp[j + 1] - } else { - newDp[j] = maxOf(dp[j], newDp[j + 1]) - } - } - dp = newDp - } - - return dp[0] - } -} +} \ No newline at end of file