Skip to content

Commit

Permalink
Knuth–Morris–Pratt algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
aladin002dz committed Oct 19, 2023
1 parent 0315c8a commit d84549a
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Bit-Manipulation/BinaryCountSetBits.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function BinaryCountSetBits(a) {

let count = 0
while (a) {
a &= (a - 1)
a &= a - 1
count++
}

Expand Down
2 changes: 1 addition & 1 deletion Maths/AutomorphicNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ export const isAutomorphic = (n) => {
n = Math.floor(n / 10)
n_sq = Math.floor(n_sq / 10)
}

return true
}
16 changes: 8 additions & 8 deletions Maths/test/AutomorphicNumber.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ describe('AutomorphicNumber', () => {
})

test.each([
{ n: -3 , expected: false },
{ n: -25 , expected: false },
{ n: -3, expected: false },
{ n: -25, expected: false }
])('should return false when n is negetive', ({ n, expected }) => {
expect(isAutomorphic(n)).toBe(false)
})

test.each([
{ n: 7 , expected: false },
{ n: 83 , expected: false },
{ n: 0 , expected: true },
{ n: 1 , expected: true },
{ n: 376 , expected: true },
{ n: 90625 , expected: true },
{ n: 7, expected: false },
{ n: 83, expected: false },
{ n: 0, expected: true },
{ n: 1, expected: true },
{ n: 376, expected: true },
{ n: 90625, expected: true }
])('should return $expected when n is $n', ({ n, expected }) => {
expect(isAutomorphic(n)).toBe(expected)
})
Expand Down
6 changes: 3 additions & 3 deletions Project-Euler/Problem006.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// https://projecteuler.net/problem=6

export const squareDifference = (num = 100) => {
let sumOfSquares = (num)*(num+1)*(2*num+1)/6
let sums = (num*(num+1))/2
let sumOfSquares = (num * (num + 1) * (2 * num + 1)) / 6
let sums = (num * (num + 1)) / 2

return sums ** 2 - sumOfSquares // difference of square of the total sum and sum of squares
}
2 changes: 1 addition & 1 deletion Search/InterpolationSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ export function interpolationSearch(arr, key) {
}

return -1
}
}
57 changes: 57 additions & 0 deletions Search/KMP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Implements the Knuth-Morris-Pratt (KMP) algorithm for pattern searching.
*
* The KMP algorithm is a string searching algorithm that utilizes the concept of prefix tables or failure functions
* to efficiently match patterns in strings. It avoids unnecessary comparisons by taking advantage of the information
* gained from previous comparisons.
*
* This implementation constructs a prefix table that stores the longest proper prefix that is also a suffix for each
* prefix of the pattern. This table is then used to guide the pattern matching process, skipping over characters that
* are known to not match.
*
* The algorithm returns the starting index of the first occurrence of the pattern in the text. If the pattern is not
* found, the algorithm returns -1.
*
* Reference: https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
*/

function KMPSearch(text, pattern) {
let lps = new Array(pattern.length).fill(0)
computeLPS(pattern, lps)
let i = 0
let j = 0
while (i < text.length) {
if (pattern[j] == text[i]) {
j++
i++
}
if (j == pattern.length) {
return i - j
} else if (i < text.length && pattern[j] != text[i]) {
if (j != 0) j = lps[j - 1]
else i = i + 1
}
}
return -1
}

function computeLPS(pattern, lps) {
let len = 0
let i = 1
while (i < pattern.length) {
if (pattern[i] == pattern[len]) {
len++
lps[i] = len
i++
} else {
if (len != 0) {
len = lps[len - 1]
} else {
lps[i] = 0
i++
}
}
}
}

export { KMPSearch }
27 changes: 27 additions & 0 deletions Search/test/KMP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { KMPSearch } from '../KMP'

describe('Rabin-Karp Search', function () {
test('KMP algorithm test 1', () => {
let text = 'AAAAA'
let pattern = 'AAA'
expect(KMPSearch(text, pattern)).toBe(0)
})

test('KMP algorithm test 2', () => {
let text = 'ABABDABACDABABCABAB'
let pattern = 'DABA'
expect(KMPSearch(text, pattern)).toBe(4)
})

test('KMP algorithm test 3', () => {
let text = 'ABABDABACDABABCABAB'
let pattern = 'ABABCABAB'
expect(KMPSearch(text, pattern)).toBe(10)
})

test('KMP algorithm test 4', () => {
let text = 'ABABDABACDABABCABAB'
let pattern = 'XYZ'
expect(KMPSearch(text, pattern)).toBe(-1)
})
})

0 comments on commit d84549a

Please sign in to comment.