Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Knuth–Morris–Pratt algorithm #1553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
aladin002dz wants to merge 1 commit into TheAlgorithms:master
base: master
Choose a base branch
Loading
from aladin002dz:KMP
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Bit-Manipulation/BinaryCountSetBits.js
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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)
})
})

AltStyle によって変換されたページ (->オリジナル) /