chore(CSharp): added longest common subsequence (#539)

Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com>
pull/556/head^2
Waqar Hassan Khan 2021-10-09 02:51:58 +06:00 committed by GitHub
parent ac25147171
commit af0e766dae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 3 deletions

View File

@ -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 | | C or C++ | @Arsenic-ATG, @UG-SEP, @aayushjain7, @Ashborn-SM |
| Java | @TawfikYasser, @cyberwizard1001, @aayushjain7 | | Java | @TawfikYasser, @cyberwizard1001, @aayushjain7 |
| C# | @ming-tsai | | C# | @ming-tsai, @Waqar-107 |
| Go | @atin | | Go | @atin |
| Python | @Arsenic-ATG, @atin, @sridhar-5, @cyberwizard1001 | | Python | @Arsenic-ATG, @atin, @sridhar-5, @cyberwizard1001 |
| JavaScript | @ming-tsai | | JavaScript | @ming-tsai |

View File

@ -4,14 +4,15 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/
## Arrays ## Arrays
1. [Single Number](src/Arrays/single-number.cs) 1. [Single Number](src/Arrays/single-number.cs)
## Dynamic Programming
1. [Longest Common Subsequence](src/Dynamic-Programming/longest-common-subsequence.cs)
## Number Theory ## Number Theory
1. [Big Mod Algorithm](src/Number-Theory/big-mod.cs) 1. [Big Mod Algorithm](src/Number-Theory/big-mod.cs)
2. [Sieve of Eratosthenes](src/Number-Theory/sieve-of-eratosthenes.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) 3. [Bitwise Sieve of Eratosthenes](src/Number-Theory/bitwise-sieve-of-eratosthenes.cs)
## Sorts ## Sorts
1. [Bubble Sort](src/Sorts/bubble-sort.cs) 1. [Bubble Sort](src/Sorts/bubble-sort.cs)
2. [Insertion Sort](src/Sorts/insertion-sort.cs) 2. [Insertion Sort](src/Sorts/insertion-sort.cs)
3. [Selection Sort](src/Sorts/selection-sort.cs) 3. [Selection Sort](src/Sorts/selection-sort.cs)

View File

@ -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)
*/

View File

@ -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));
}
}
}