Skip to content

Commit d84549a

Browse files
committed
Knuth–Morris–Pratt algorithm
1 parent 0315c8a commit d84549a

File tree

7 files changed

+98
-14
lines changed

7 files changed

+98
-14
lines changed

Bit-Manipulation/BinaryCountSetBits.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function BinaryCountSetBits(a) {
1616

1717
let count = 0
1818
while (a) {
19-
a &= (a - 1)
19+
a &= a - 1
2020
count++
2121
}
2222

Maths/AutomorphicNumber.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ export const isAutomorphic = (n) => {
3535
n = Math.floor(n / 10)
3636
n_sq = Math.floor(n_sq / 10)
3737
}
38-
38+
3939
return true
4040
}

Maths/test/AutomorphicNumber.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ describe('AutomorphicNumber', () => {
99
})
1010

1111
test.each([
12-
{ n: -3 , expected: false },
13-
{ n: -25 , expected: false },
12+
{ n: -3, expected: false },
13+
{ n: -25, expected: false }
1414
])('should return false when n is negetive', ({ n, expected }) => {
1515
expect(isAutomorphic(n)).toBe(false)
1616
})
1717

1818
test.each([
19-
{ n: 7 , expected: false },
20-
{ n: 83 , expected: false },
21-
{ n: 0 , expected: true },
22-
{ n: 1 , expected: true },
23-
{ n: 376 , expected: true },
24-
{ n: 90625 , expected: true },
19+
{ n: 7, expected: false },
20+
{ n: 83, expected: false },
21+
{ n: 0, expected: true },
22+
{ n: 1, expected: true },
23+
{ n: 376, expected: true },
24+
{ n: 90625, expected: true }
2525
])('should return $expected when n is $n', ({ n, expected }) => {
2626
expect(isAutomorphic(n)).toBe(expected)
2727
})

Project-Euler/Problem006.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// https://projecteuler.net/problem=6
22

33
export const squareDifference = (num = 100) => {
4-
let sumOfSquares = (num)*(num+1)*(2*num+1)/6
5-
let sums = (num*(num+1))/2
6-
4+
let sumOfSquares = (num * (num + 1) * (2 * num + 1)) / 6
5+
let sums = (num * (num + 1)) / 2
6+
77
return sums ** 2 - sumOfSquares // difference of square of the total sum and sum of squares
88
}

Search/InterpolationSearch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ export function interpolationSearch(arr, key) {
3636
}
3737

3838
return -1
39-
}
39+
}

Search/KMP.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Implements the Knuth-Morris-Pratt (KMP) algorithm for pattern searching.
3+
*
4+
* The KMP algorithm is a string searching algorithm that utilizes the concept of prefix tables or failure functions
5+
* to efficiently match patterns in strings. It avoids unnecessary comparisons by taking advantage of the information
6+
* gained from previous comparisons.
7+
*
8+
* This implementation constructs a prefix table that stores the longest proper prefix that is also a suffix for each
9+
* prefix of the pattern. This table is then used to guide the pattern matching process, skipping over characters that
10+
* are known to not match.
11+
*
12+
* The algorithm returns the starting index of the first occurrence of the pattern in the text. If the pattern is not
13+
* found, the algorithm returns -1.
14+
*
15+
* Reference: https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
16+
*/
17+
18+
function KMPSearch(text, pattern) {
19+
let lps = new Array(pattern.length).fill(0)
20+
computeLPS(pattern, lps)
21+
let i = 0
22+
let j = 0
23+
while (i < text.length) {
24+
if (pattern[j] == text[i]) {
25+
j++
26+
i++
27+
}
28+
if (j == pattern.length) {
29+
return i - j
30+
} else if (i < text.length && pattern[j] != text[i]) {
31+
if (j != 0) j = lps[j - 1]
32+
else i = i + 1
33+
}
34+
}
35+
return -1
36+
}
37+
38+
function computeLPS(pattern, lps) {
39+
let len = 0
40+
let i = 1
41+
while (i < pattern.length) {
42+
if (pattern[i] == pattern[len]) {
43+
len++
44+
lps[i] = len
45+
i++
46+
} else {
47+
if (len != 0) {
48+
len = lps[len - 1]
49+
} else {
50+
lps[i] = 0
51+
i++
52+
}
53+
}
54+
}
55+
}
56+
57+
export { KMPSearch }

Search/test/KMP.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { KMPSearch } from '../KMP'
2+
3+
describe('Rabin-Karp Search', function () {
4+
test('KMP algorithm test 1', () => {
5+
let text = 'AAAAA'
6+
let pattern = 'AAA'
7+
expect(KMPSearch(text, pattern)).toBe(0)
8+
})
9+
10+
test('KMP algorithm test 2', () => {
11+
let text = 'ABABDABACDABABCABAB'
12+
let pattern = 'DABA'
13+
expect(KMPSearch(text, pattern)).toBe(4)
14+
})
15+
16+
test('KMP algorithm test 3', () => {
17+
let text = 'ABABDABACDABABCABAB'
18+
let pattern = 'ABABCABAB'
19+
expect(KMPSearch(text, pattern)).toBe(10)
20+
})
21+
22+
test('KMP algorithm test 4', () => {
23+
let text = 'ABABDABACDABABCABAB'
24+
let pattern = 'XYZ'
25+
expect(KMPSearch(text, pattern)).toBe(-1)
26+
})
27+
})

0 commit comments

Comments
 (0)