diff --git a/README.md b/README.md index 911e5021..9b9a3547 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ The programming should keep the naming convention rule of each programming langu | -------------------- | ------------------------------------------------- | | C or C++ | @Arsenic-ATG, @UG-SEP, @aayushjain7, @Ashborn-SM | | Java | @TawfikYasser, @cyberwizard1001, @aayushjain7 | -| C# | @ming-tsai | +| C# | @ming-tsai, @Waqar-107 | | Go | @atin | | Python | @Arsenic-ATG, @atin, @sridhar-5, @cyberwizard1001 | | JavaScript | @ming-tsai | diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index ebe9e827..e61fff98 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -4,14 +4,15 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/ ## Arrays 1. [Single Number](src/Arrays/single-number.cs) +## Dynamic Programming +1. [Longest Common Subsequence](src/Dynamic-Programming/longest-common-subsequence.cs) + ## Number Theory 1. [Big Mod Algorithm](src/Number-Theory/big-mod.cs) 2. [Sieve of Eratosthenes](src/Number-Theory/sieve-of-eratosthenes.cs) 3. [Bitwise Sieve of Eratosthenes](src/Number-Theory/bitwise-sieve-of-eratosthenes.cs) - ## Sorts - 1. [Bubble Sort](src/Sorts/bubble-sort.cs) 2. [Insertion Sort](src/Sorts/insertion-sort.cs) 3. [Selection Sort](src/Sorts/selection-sort.cs) diff --git a/algorithms/CSharp/src/Dynamic-Programming/longest-common-subsequence.cs b/algorithms/CSharp/src/Dynamic-Programming/longest-common-subsequence.cs new file mode 100644 index 00000000..3742fd6e --- /dev/null +++ b/algorithms/CSharp/src/Dynamic-Programming/longest-common-subsequence.cs @@ -0,0 +1,51 @@ +using System; + +namespace Algorithms.DynamicProgramming +{ + public class LongestCommonSubsequence + { + public static int LCS(string word1, string word2) + { + int len1 = word1.Length, len2 = word2.Length; + int[,] dp = new int[2, len2 + 1]; + + for (int i = 0; i < 2; i++) + { + for (int j = 0; j <= len2; j++) + { + dp[i, j] = 0; + } + } + + int currentRow = 1; + for (int i = 1; i <= len1; i++) + { + for (int j = 1; j <= len2; j++) + { + if (word1[i - 1] == word2[j - 1]) + { + dp[currentRow, j] = 1 + dp[1 - currentRow, j - 1]; + } + else + { + dp[currentRow, j] = Math.Max(dp[1 - currentRow, j], dp[currentRow, j - 1]); + } + } + + currentRow = 1 - currentRow; + } + + return dp[1 - currentRow, len2]; + } + + public static void Main() + { + Console.WriteLine(LCS("AGGTA", "GXTXAY")); + } + } +} + +/* + * Time complexity: O(nm) + * Space complexity: O(m) + */ \ No newline at end of file diff --git a/algorithms/CSharp/test/Dynamic-Programming/longest-common-subsequence.cs b/algorithms/CSharp/test/Dynamic-Programming/longest-common-subsequence.cs new file mode 100644 index 00000000..b41020d0 --- /dev/null +++ b/algorithms/CSharp/test/Dynamic-Programming/longest-common-subsequence.cs @@ -0,0 +1,17 @@ +using NUnit.Framework; + +namespace Algorithms.Tests.DynamicProgramming +{ + [TestFixture] + public class LongestCommonSubsequence + { + [TestCase("AGGTA", "GXTXAY", 3)] + [TestCase("ABCDGH", "AEDFHR", 3)] + [TestCase("ABC", "AB", 2)] + [TestCase("AEBEEBCCBACA", "CEACEBEBCBAA", 9)] + public void TestLCS_ShouldGetExpectedResult(string word1, string word2, int expected) + { + Assert.AreEqual(expected, Algorithms.DynamicProgramming.LongestCommonSubsequence.LCS(word1, word2)); + } + } +}