-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -16,7 +16,7 @@ function BinaryCountSetBits(a) { | |
|
||
let count = 0 | ||
while (a) { | ||
a &= (a - 1) | ||
a &= a - 1 | ||
count++ | ||
} | ||
|
||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -35,6 +35,6 @@ export const isAutomorphic = (n) => { | |
n = Math.floor(n / 10) | ||
n_sq = Math.floor(n_sq / 10) | ||
} | ||
|
||
return true | ||
} |
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -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 | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -36,4 +36,4 @@ export function interpolationSearch(arr, key) { | |
} | ||
|
||
return -1 | ||
} | ||
} |
This file contains hidden or 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,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 } |
This file contains hidden or 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,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) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.