From 9f094db1406c7468d6c9395dd1900e7791654dd9 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sun, 3 Nov 2024 19:03:02 +0800 Subject: [PATCH] 249th Commit --- README.md | 48 ++++++++++--------- .../longestCommonSubsequence.test.ts | 9 ++++ .../longestCommonSubsequence.ts | 26 ++++++++++ 3 files changed, 60 insertions(+), 23 deletions(-) create mode 100644 src/page-11/1143. Longest Common Subsequence/longestCommonSubsequence.test.ts create mode 100644 src/page-11/1143. Longest Common Subsequence/longestCommonSubsequence.ts diff --git a/README.md b/README.md index a3af1e8..c5f0eaa 100644 --- a/README.md +++ b/README.md @@ -252,14 +252,15 @@ Ace Coding Interview with 75 Qs [198]: ./src/page-2/198.%20House%20Robber/rob.ts [790]: ./src/page-8/790.%20Domino%20and%20Tromino%20Tiling/numTilings.ts -| DP - Multidimensional | | | -| --------------------------------------------------------- | -------------- | ------ | -| 62. Unique Paths | [Solution][62] | Medium | -| 1143. Longest Common Subsequence | Solution | Medium | -| 714. Best Time to Buy and Sell Stock with Transaction Fee | Solution | Medium | -| 72. Edit Distance | Solution | Medium | +| DP - Multidimensional | | | +| --------------------------------------------------------- | ---------------- | ------ | +| 62. Unique Paths | [Solution][62] | Medium | +| 1143. Longest Common Subsequence | [Solution][1143] | Medium | +| 714. Best Time to Buy and Sell Stock with Transaction Fee | Solution | Medium | +| 72. Edit Distance | Solution | Medium | [62]: ./src/page-1/62.%20Unique%20Paths/uniquePaths.ts +[1143]: ./src/page-11/1143.%20Longest%20Common%20Subsequence/longestCommonSubsequence.ts | Bit Manipulation | | | | --------------------------------------------- | ---------------- | ------ | @@ -645,29 +646,30 @@ Must-do List for Interview Prep [236]: ./src/page-3/236.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/lowestCommonAncestor.ts [437]: ./src/page-5/437.%20Path%20Sum%20III/pathSum.ts -| Dynamic Programming | | | -| ----------------------------------- | --------------- | ------ | -| 5. Longest Palindromic Substring | [Solution][5] | Medium | -| 32. Longest Valid Parentheses | Solution | Hard | -| 62. Unique Paths | [Solution][62] | Medium | -| 64. Minimum Path Sum | Solution | Medium | -| 70. Climbing Stairs | [Solution][70] | Easy | -| 72. Edit Distance | Solution | Medium | -| 118. Pascal's Triangle | [Solution][118] | Easy | -| 139. Word Break | Solution | Medium | -| 152. Maximum Product Subarray | Solution | Medium | -| 198. House Robber | [Solution][198] | Medium | -| 279. Perfect Squares | Solution | Medium | -| 300. Longest Increasing Subsequence | Solution | Medium | -| 322. Coin Change | Solution | Medium | -| 416. Partition Equal Subset Sum | Solution | Medium | -| 1143. Longest Common Subsequence | Solution | Medium | +| Dynamic Programming | | | +| ----------------------------------- | ---------------- | ------ | +| 5. Longest Palindromic Substring | [Solution][5] | Medium | +| 32. Longest Valid Parentheses | Solution | Hard | +| 62. Unique Paths | [Solution][62] | Medium | +| 64. Minimum Path Sum | Solution | Medium | +| 70. Climbing Stairs | [Solution][70] | Easy | +| 72. Edit Distance | Solution | Medium | +| 118. Pascal's Triangle | [Solution][118] | Easy | +| 139. Word Break | Solution | Medium | +| 152. Maximum Product Subarray | Solution | Medium | +| 198. House Robber | [Solution][198] | Medium | +| 279. Perfect Squares | Solution | Medium | +| 300. Longest Increasing Subsequence | Solution | Medium | +| 322. Coin Change | Solution | Medium | +| 416. Partition Equal Subset Sum | Solution | Medium | +| 1143. Longest Common Subsequence | [Solution][1143] | Medium | [5]: ./src/page-1/5.%20Longest%20Palindromic%20Substring/longestPalindrome.ts [62]: ./src/page-1/62.%20Unique%20Paths/uniquePaths.ts [70]: ./src/page-1/70.%20Climbing%20Stairs/climbStairs.ts [118]: ./src/page-2/118.%20Pascal's%20Triangle/generate.ts [198]: ./src/page-2/198.%20House%20Robber/rob.ts +[1143]: ./src/page-11/1143.%20Longest%20Common%20Subsequence/longestCommonSubsequence.ts | Graph | | | | ---------------------- | --------------- | ------ | diff --git a/src/page-11/1143. Longest Common Subsequence/longestCommonSubsequence.test.ts b/src/page-11/1143. Longest Common Subsequence/longestCommonSubsequence.test.ts new file mode 100644 index 0000000..33e424b --- /dev/null +++ b/src/page-11/1143. Longest Common Subsequence/longestCommonSubsequence.test.ts @@ -0,0 +1,9 @@ +import { longestCommonSubsequence } from './longestCommonSubsequence'; + +describe('1143. Longest Common Subsequence', () => { + test('longestCommonSubsequence', () => { + expect(longestCommonSubsequence('abcde', 'ace')).toBe(3); + expect(longestCommonSubsequence('abc', 'abc')).toBe(3); + expect(longestCommonSubsequence('abc', 'def')).toBe(0); + }); +}); diff --git a/src/page-11/1143. Longest Common Subsequence/longestCommonSubsequence.ts b/src/page-11/1143. Longest Common Subsequence/longestCommonSubsequence.ts new file mode 100644 index 0000000..711b4a9 --- /dev/null +++ b/src/page-11/1143. Longest Common Subsequence/longestCommonSubsequence.ts @@ -0,0 +1,26 @@ +type LongestCommonSubsequence = (text1: string, text2: string) => number; + +/** + * Accepted + */ +export const longestCommonSubsequence: LongestCommonSubsequence = (text1, text2) => { + const m = text1.length; + const n = text2.length; + + // Initialize a (m+1) x (n+1) DP array with zeros + const dp: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + + // Fill in the DP array + for (let i = 1; i <= m; i++) { + for (let j = 1; j <= n; j++) { + if (text1[i - 1] === text2[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); + } + } + } + + // The length of the longest common subsequence is found at dp[m][n] + return dp[m][n]; +};