diff --git a/Bit-Manipulation/BinaryCountSetBits.js b/Bit-Manipulation/BinaryCountSetBits.js index b879f3bd67..b959caf062 100644 --- a/Bit-Manipulation/BinaryCountSetBits.js +++ b/Bit-Manipulation/BinaryCountSetBits.js @@ -16,7 +16,7 @@ function BinaryCountSetBits(a) { let count = 0 while (a) { - a &= (a - 1) + a &= a - 1 count++ } diff --git a/DIRECTORY.md b/DIRECTORY.md index aea7b1548f..cd4d244e31 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -166,6 +166,7 @@ * [Area](Maths/Area.js) * [ArithmeticGeometricMean](Maths/ArithmeticGeometricMean.js) * [ArmstrongNumber](Maths/ArmstrongNumber.js) + * [AutomorphicNumber](Maths/AutomorphicNumber.js) * [AverageMean](Maths/AverageMean.js) * [AverageMedian](Maths/AverageMedian.js) * [BinaryConvert](Maths/BinaryConvert.js) @@ -308,6 +309,7 @@ * [LinearSearch](Search/LinearSearch.js) * [Minesweeper](Search/Minesweeper.js) * [QuickSelectSearch](Search/QuickSelectSearch.js) + * [RabinKarp](Search/RabinKarp.js) * [SlidingWindow](Search/SlidingWindow.js) * [StringSearch](Search/StringSearch.js) * [TernarySearch](Search/TernarySearch.js) diff --git a/Maths/AutomorphicNumber.js b/Maths/AutomorphicNumber.js index d1b6608316..ba008271fc 100644 --- a/Maths/AutomorphicNumber.js +++ b/Maths/AutomorphicNumber.js @@ -35,6 +35,6 @@ export const isAutomorphic = (n) => { n = Math.floor(n / 10) n_sq = Math.floor(n_sq / 10) } - + return true } diff --git a/Maths/test/AutomorphicNumber.test.js b/Maths/test/AutomorphicNumber.test.js index 19b963388c..57f40d27ee 100644 --- a/Maths/test/AutomorphicNumber.test.js +++ b/Maths/test/AutomorphicNumber.test.js @@ -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) }) diff --git a/Search/InterpolationSearch.js b/Search/InterpolationSearch.js index e6deae496f..93f3b78b0e 100644 --- a/Search/InterpolationSearch.js +++ b/Search/InterpolationSearch.js @@ -36,4 +36,4 @@ export function interpolationSearch(arr, key) { } return -1 -} \ No newline at end of file +} diff --git a/Search/RabinKarp.js b/Search/RabinKarp.js new file mode 100644 index 0000000000..e6d6394ede --- /dev/null +++ b/Search/RabinKarp.js @@ -0,0 +1,64 @@ +/* + * Implements the Rabin-Karp algorithm for pattern searching. + * + * The Rabin-Karp algorithm is a string searching algorithm that uses hashing to find patterns in strings. + * It is faster than naive string matching algorithms because it avoids comparing every character in the text. + * + * This implementation uses a rolling hash function to efficiently compute the hash values of substrings. + * It also uses a modulo operator to reduce the size of the hash values, which helps to prevent hash collisions. + * + * The algorithm returns an array of indices where the pattern is found in the text. If the pattern is not + * found, the algorithm returns an empty array. + * + * [Reference](https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm) + */ + +const BASE = 256 // The number of characters in the alphabet +const MOD = 997 // A prime number used for the hash function + +function rabinKarpSearch(text, pattern) { + const patternLength = pattern.length + const textLength = text.length + const hashPattern = hash(pattern, patternLength) + const hashText = [] + const indices = [] + + // Calculate the hash of the first substring in the text + hashText[0] = hash(text, patternLength) + + // Precompute BASE^(patternLength-1) % MOD + const basePow = Math.pow(BASE, patternLength - 1) % MOD + + for (let i = 1; i <= textLength - patternLength + 1; i++) { + // Update the rolling hash by removing the first character + // and adding the next character in the text + hashText[i] = + (BASE * (hashText[i - 1] - text.charCodeAt(i - 1) * basePow) + + text.charCodeAt(i + patternLength - 1)) % + MOD + + // In case of hash collision, check character by character + if (hashText[i] < 0) { + hashText[i] += MOD + } + + // Check if the hashes match and perform a character-wise comparison + if (hashText[i] === hashPattern) { + if (text.substring(i, i + patternLength) === pattern) { + indices.push(i) // Store the index where the pattern is found + } + } + } + + return indices +} + +function hash(str, length) { + let hashValue = 0 + for (let i = 0; i < length; i++) { + hashValue = (hashValue * BASE + str.charCodeAt(i)) % MOD + } + return hashValue +} + +export { rabinKarpSearch } diff --git a/Search/test/RabinKarp.test.js b/Search/test/RabinKarp.test.js new file mode 100644 index 0000000000..3d10097509 --- /dev/null +++ b/Search/test/RabinKarp.test.js @@ -0,0 +1,30 @@ +import { rabinKarpSearch } from '../RabinKarp' + +describe('Rabin-Karp Search', function () { + it('should find the pattern in the text', function () { + const text = 'ABABDABACDABABCABAB' + const pattern = 'DAB' + const expected = [4, 9] + + const result = rabinKarpSearch(text, pattern) + expect(result).to.deep.equal(expected) + }) + + it('should handle multiple occurrences of the pattern', function () { + const text = 'ABABABABABAB' + const pattern = 'ABAB' + const expected = [2, 4, 6, 8] + + const result = rabinKarpSearch(text, pattern) + expect(result).to.deep.equal(expected) + }) + + it('should handle pattern not found', function () { + const text = 'ABCD' + const pattern = 'XYZ' + const expected = [] + + const result = rabinKarpSearch(text, pattern) + expect(result).to.deep.equal(expected) + }) +})