forked from Sharpach/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding KMP and adding rerevelant unit test.
Resolving Sharpach#9
- Loading branch information
Zohaib Sharani
committed
Oct 2, 2018
1 parent
9e7288d
commit e4b879d
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
Algorithms/Algorithms.Tests/KnuthMorrisPrattTests/KnuthMorrisPrattTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Xunit; | ||
|
||
namespace Algorithms.Tests | ||
{ | ||
public class KnuthMorrisPrattTests | ||
{ | ||
[Fact] | ||
public void FunctionalityCheck() | ||
{ | ||
var result = KnuthMorrisPratt.KnuthMorrisPratt.KMPSearch("AKMPAKMPCAADAKMPAKMPAA", "AKMPA"); | ||
Assert.NotEmpty(result); | ||
Assert.Equal(3, result.Count); | ||
Assert.Equal(0, result[0]); | ||
Assert.Equal(12, result[1]); | ||
Assert.Equal(16, result[2]); | ||
} | ||
|
||
[Fact] | ||
public void EmptyCheck() | ||
{ | ||
var result = KnuthMorrisPratt.KnuthMorrisPratt.KMPSearch("AKMPACAADAKMPKMP", "ASD"); | ||
Assert.Empty(result); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
Algorithms/Algorithms/KnuthMorrisPratt/KnuthMorrisPratt.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Algorithms.KnuthMorrisPratt | ||
{ | ||
public class KnuthMorrisPratt | ||
{ | ||
public static List<int> KMPSearch(string text, string searchWord) | ||
{ | ||
var result = new List<int>(); | ||
var txtLength = text.Length; | ||
var wordLength = searchWord.Length; | ||
|
||
int[] prefixPattren = new int[wordLength]; | ||
LongestPrefixPattren(searchWord, prefixPattren); | ||
|
||
int wordIterator = 0; | ||
int textIterator = 0; | ||
while (textIterator<txtLength) | ||
{ | ||
if (searchWord[wordIterator] == text[textIterator]) | ||
{ | ||
textIterator++; | ||
wordIterator++; | ||
} | ||
|
||
if (wordIterator == wordLength) | ||
{ | ||
result.Add(textIterator - wordIterator); | ||
wordIterator = prefixPattren[wordIterator - 1]; | ||
} | ||
else if (textIterator < txtLength && searchWord[wordIterator] != text[textIterator]) | ||
{ | ||
if (wordIterator != 0) | ||
{ | ||
wordIterator = prefixPattren[wordIterator - 1]; | ||
} | ||
else | ||
{ | ||
textIterator++; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private static void LongestPrefixPattren(string searchWord, int[] prefixPattren) | ||
{ | ||
var len = 0; | ||
var i = 1; | ||
prefixPattren[0] = 0; | ||
var searchWordLength = searchWord.Length; | ||
|
||
|
||
while (i< searchWordLength) | ||
{ | ||
if (searchWord[i] == searchWord[len]) | ||
{ | ||
len++; | ||
prefixPattren[i] = len; | ||
i++; | ||
} | ||
else | ||
{ | ||
if (len != 0) | ||
{ | ||
len = prefixPattren[len - 1]; | ||
} | ||
else | ||
{ | ||
prefixPattren[i] = len; | ||
i++; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |