From 277464f28ea41efa5300fb7b5e7e97c6c7bbff42 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 24 Mar 2022 00:26:13 +0000 Subject: [PATCH 01/11] Auto-update DIRECTORY.md --- DIRECTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 927b3b1bde..8c66cafc65 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -18,6 +18,8 @@ * [LFUCache](https://github.com/TheAlgorithms/Javascript/blob/master/Cache/LFUCache.js) * [LRUCache](https://github.com/TheAlgorithms/Javascript/blob/master/Cache/LRUCache.js) * [Memoize](https://github.com/TheAlgorithms/Javascript/blob/master/Cache/Memoize.js) + * test + * [cacheTest](https://github.com/TheAlgorithms/Javascript/blob/master/Cache/test/cacheTest.js) ## Cellular-Automata * [ConwaysGameOfLife](https://github.com/TheAlgorithms/Javascript/blob/master/Cellular-Automata/ConwaysGameOfLife.js) @@ -70,6 +72,7 @@ * [MinHeap](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Heap/MinHeap.js) * [MinPriorityQueue](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Heap/MinPriorityQueue.js) * Linked-List + * [AddTwoNumbers](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/AddTwoNumbers.js) * [CycleDetection](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/CycleDetection.js) * [DoublyLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/DoublyLinkedList.js) * [RotateListRight](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/RotateListRight.js) @@ -143,6 +146,7 @@ * [Abs](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Abs.js) * [AliquotSum](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/AliquotSum.js) * [Area](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Area.js) + * [ArithmeticGeometricMean](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ArithmeticGeometricMean.js) * [ArmstrongNumber](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ArmstrongNumber.js) * [AverageMean](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/AverageMean.js) * [AverageMedian](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/AverageMedian.js) @@ -168,6 +172,7 @@ * [FindHcf](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindHcf.js) * [FindLcm](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindLcm.js) * [FindMin](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindMin.js) + * [FindMinIterator](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindMinIterator.js) * [GetEuclidGCD](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/GetEuclidGCD.js) * [GridGet](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/GridGet.js) * [IsDivisible](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/IsDivisible.js) @@ -290,6 +295,7 @@ * [AlternativeStringArrange](https://github.com/TheAlgorithms/Javascript/blob/master/String/AlternativeStringArrange.js) * [CheckAnagram](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckAnagram.js) * [CheckCamelCase](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckCamelCase.js) + * [CheckExceeding](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckExceeding.js) * [CheckFlatCase](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckFlatCase.js) * [CheckKebabCase](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckKebabCase.js) * [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckPalindrome.js) From 2e63444be79b9da92a5eb0e6dae5368a303d1c98 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 20 Apr 2022 21:32:49 +0000 Subject: [PATCH 02/11] Auto-update DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 8613c684c8..c9b08760af 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -77,7 +77,7 @@ * [CycleDetection](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/CycleDetection.js) * [DoublyLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/DoublyLinkedList.js) * [RotateListRight](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/RotateListRight.js) - * [SingleCircularLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/SingleCircularLinkedList.js.js) + * [SinglyCircularLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/SinglyCircularLinkedList.js) * [SinglyLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/SinglyLinkedList.js) * Queue * [CircularQueue](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Queue/CircularQueue.js) From e7cb578cb309b94905cba464696924900ab436b3 Mon Sep 17 00:00:00 2001 From: Ayoade David Date: Wed, 20 Apr 2022 22:37:11 +0100 Subject: [PATCH 03/11] Created boyerMoore.js Added boyerMoore string searching algorithm --- String/boyerMoore.js | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 String/boyerMoore.js diff --git a/String/boyerMoore.js b/String/boyerMoore.js new file mode 100644 index 0000000000..41782226cc --- /dev/null +++ b/String/boyerMoore.js @@ -0,0 +1,58 @@ + /** + * + * + *Implementation of the Boyer-Moore String Search Algo. + *The Boyer–Moore string search algorithm allows linear time in + * search by skipping + * indices when searching inside a string for a pattern. Fo + * + * + * + * + * */ +let buildBadMatchTable = (str) => { + var tableObj = {}, + strLength = str.length; + for (var i = 0; i < strLength - 1; i++) { + tableObj[str[i]] = strLength - 1 - i; + } + if (tableObj[str[strLength-1]] == undefined) { + tableObj[str[strLength-1]] = strLength; + } + return tableObj; +} + + +let boyerMoore = (str, pattern)=> { + var badMatchTable = buildBadMatchTable(pattern), + offset = 0, + patternLastIndex = pattern.length - 1, + scanIndex = patternLastIndex, + maxOffset = str.length - pattern.length; + // if the offset is bigger than maxOffset, cannot be found + while (offset <= maxOffset) { + scanIndex = 0; + while (pattern[scanIndex] == str[scanIndex + offset]) { + if (scanIndex == patternLastIndex) { + // found at this index + return offset; + } + scanIndex++; + } + var badMatchString = str[offset + patternLastIndex]; + if (badMatchTable[badMatchString]) { + // increase the offset if it exists + offset += badMatchTable[badMatchString] + } else { + offset += 1; + } + } + return -1; +} +export {boyerMoore} + + + + + + From 8111a439a338e6cf8c78a8837e2d714e2648010e Mon Sep 17 00:00:00 2001 From: Ayoade David Date: Wed, 20 Apr 2022 22:52:47 +0100 Subject: [PATCH 04/11] Update and rename boyerMoore.js to BoyerMoore.js and fix code style. --- String/BoyerMoore.js | 58 ++++++++++++++++++++++++++++++++++++++++++++ String/boyerMoore.js | 58 -------------------------------------------- 2 files changed, 58 insertions(+), 58 deletions(-) create mode 100644 String/BoyerMoore.js delete mode 100644 String/boyerMoore.js diff --git a/String/BoyerMoore.js b/String/BoyerMoore.js new file mode 100644 index 0000000000..28e68e12fc --- /dev/null +++ b/String/BoyerMoore.js @@ -0,0 +1,58 @@ + /* + * + * + *Implementation of the Boyer-Moore String Search Algo. + *The Boyer–Moore string search algorithm allows linear time in + * search by skipping + * indices when searching inside a string for a pattern. + * + * + * + * + **/ +const buildBadMatchTable = (str) => { + let tableObj = {}; + let strLength = str.length; + for (var i = 0; i < strLength - 1; i++) { + tableObj[str[i]] = strLength - 1 - i; + } + if (tableObj[str[strLength-1]] == undefined) { + tableObj[str[strLength-1]] = strLength; + } + return tableObj; +} + + +let boyerMoore = (str, pattern)=> { + var badMatchTable = buildBadMatchTable(pattern), + offset = 0, + patternLastIndex = pattern.length - 1, + scanIndex = patternLastIndex, + maxOffset = str.length - pattern.length; + // if the offset is bigger than maxOffset, cannot be found + while (offset <= maxOffset) { + scanIndex = 0; + while (pattern[scanIndex] == str[scanIndex + offset]) { + if (scanIndex == patternLastIndex) { + // found at this index + return offset; + } + scanIndex++; + } + var badMatchString = str[offset + patternLastIndex]; + if (badMatchTable[badMatchString]) { + // increase the offset if it exists + offset += badMatchTable[badMatchString] + } else { + offset += 1; + } + } + return -1; +} +export {boyerMoore} + + + + + + diff --git a/String/boyerMoore.js b/String/boyerMoore.js deleted file mode 100644 index 41782226cc..0000000000 --- a/String/boyerMoore.js +++ /dev/null @@ -1,58 +0,0 @@ - /** - * - * - *Implementation of the Boyer-Moore String Search Algo. - *The Boyer–Moore string search algorithm allows linear time in - * search by skipping - * indices when searching inside a string for a pattern. Fo - * - * - * - * - * */ -let buildBadMatchTable = (str) => { - var tableObj = {}, - strLength = str.length; - for (var i = 0; i < strLength - 1; i++) { - tableObj[str[i]] = strLength - 1 - i; - } - if (tableObj[str[strLength-1]] == undefined) { - tableObj[str[strLength-1]] = strLength; - } - return tableObj; -} - - -let boyerMoore = (str, pattern)=> { - var badMatchTable = buildBadMatchTable(pattern), - offset = 0, - patternLastIndex = pattern.length - 1, - scanIndex = patternLastIndex, - maxOffset = str.length - pattern.length; - // if the offset is bigger than maxOffset, cannot be found - while (offset <= maxOffset) { - scanIndex = 0; - while (pattern[scanIndex] == str[scanIndex + offset]) { - if (scanIndex == patternLastIndex) { - // found at this index - return offset; - } - scanIndex++; - } - var badMatchString = str[offset + patternLastIndex]; - if (badMatchTable[badMatchString]) { - // increase the offset if it exists - offset += badMatchTable[badMatchString] - } else { - offset += 1; - } - } - return -1; -} -export {boyerMoore} - - - - - - From 81e72f61b64c6f0297bc71c8a9b0b4de768a772e Mon Sep 17 00:00:00 2001 From: Ayoade David Date: Thu, 21 Apr 2022 23:17:58 +0100 Subject: [PATCH 05/11] Update BooyerMoore.js --- String/BoyerMoore.js | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/String/BoyerMoore.js b/String/BoyerMoore.js index 28e68e12fc..3b4944d3bc 100644 --- a/String/BoyerMoore.js +++ b/String/BoyerMoore.js @@ -1,4 +1,4 @@ - /* +/* * * *Implementation of the Boyer-Moore String Search Algo. @@ -11,24 +11,24 @@ * **/ const buildBadMatchTable = (str) => { - let tableObj = {}; - let strLength = str.length; - for (var i = 0; i < strLength - 1; i++) { - tableObj[str[i]] = strLength - 1 - i; + const tableObj = {}; + const strLength = str.length; + for (let i = 0; i < strLength - 1; i++) { + tableObj[str[i]] = strLength - 1 - i; } - if (tableObj[str[strLength-1]] == undefined) { - tableObj[str[strLength-1]] = strLength; + if (tableObj[str[strLength - 1]] === undefined) { + tableObj[str[strLength - 1]] = strLength; } return tableObj; } -let boyerMoore = (str, pattern)=> { - var badMatchTable = buildBadMatchTable(pattern), - offset = 0, - patternLastIndex = pattern.length - 1, - scanIndex = patternLastIndex, - maxOffset = str.length - pattern.length; +const boyerMoore = (str, pattern) => { + let badMatchTable = buildBadMatchTable(pattern); + let offset = 0; + let patternLastIndex = pattern.length - 1; + let scanIndex = patternLastIndex; + let maxOffset = str.length - pattern.length; // if the offset is bigger than maxOffset, cannot be found while (offset <= maxOffset) { scanIndex = 0; @@ -39,7 +39,7 @@ let boyerMoore = (str, pattern)=> { } scanIndex++; } - var badMatchString = str[offset + patternLastIndex]; + const badMatchString = str[offset + patternLastIndex]; if (badMatchTable[badMatchString]) { // increase the offset if it exists offset += badMatchTable[badMatchString] @@ -49,10 +49,4 @@ let boyerMoore = (str, pattern)=> { } return -1; } -export {boyerMoore} - - - - - - +export { boyerMoore } From 30f748615b4bff40d30a8810ab05b7c1c995455e Mon Sep 17 00:00:00 2001 From: Ayoade David Date: Thu, 21 Apr 2022 23:42:01 +0100 Subject: [PATCH 06/11] Update BoyerMoore.js --- String/BoyerMoore.js | 53 ++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/String/BoyerMoore.js b/String/BoyerMoore.js index 3b4944d3bc..84d6fff30b 100644 --- a/String/BoyerMoore.js +++ b/String/BoyerMoore.js @@ -11,42 +11,41 @@ * **/ const buildBadMatchTable = (str) => { - const tableObj = {}; - const strLength = str.length; + const tableObj = {} + const strLength = str.length for (let i = 0; i < strLength - 1; i++) { - tableObj[str[i]] = strLength - 1 - i; + tableObj[str[i]] = strLength - 1 - i } if (tableObj[str[strLength - 1]] === undefined) { - tableObj[str[strLength - 1]] = strLength; + tableObj[str[strLength - 1]] = strLength } - return tableObj; + return tableObj } - const boyerMoore = (str, pattern) => { - let badMatchTable = buildBadMatchTable(pattern); - let offset = 0; - let patternLastIndex = pattern.length - 1; - let scanIndex = patternLastIndex; - let maxOffset = str.length - pattern.length; + const badMatchTable = buildBadMatchTable(pattern) + let offset = 0 + const patternLastIndex = pattern.length - 1 + let scanIndex = patternLastIndex + const maxOffset = str.length - pattern.length // if the offset is bigger than maxOffset, cannot be found while (offset <= maxOffset) { - scanIndex = 0; - while (pattern[scanIndex] == str[scanIndex + offset]) { - if (scanIndex == patternLastIndex) { - // found at this index - return offset; - } - scanIndex++; - } - const badMatchString = str[offset + patternLastIndex]; - if (badMatchTable[badMatchString]) { - // increase the offset if it exists - offset += badMatchTable[badMatchString] - } else { - offset += 1; - } + scanIndex = 0 + while (pattern[scanIndex] === str[scanIndex + offset]) { + if (scanIndex === patternLastIndex) { + // found at this index + return offset + } + scanIndex++ + } + const badMatchString = str[offset + patternLastIndex] + if (badMatchTable[badMatchString]) { + // increase the offset if it exists + offset += badMatchTable[badMatchString] + } else { + offset += 1 + } } - return -1; + return -1 } export { boyerMoore } From 95a902acb9244d2e9c8683dc6d6a623e95c8fc9a Mon Sep 17 00:00:00 2001 From: Ayoade David Date: Thu, 21 Apr 2022 23:45:28 +0100 Subject: [PATCH 07/11] Final fix codestyle for BoyerMoore.js --- String/BoyerMoore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/BoyerMoore.js b/String/BoyerMoore.js index 84d6fff30b..e25a152a1d 100644 --- a/String/BoyerMoore.js +++ b/String/BoyerMoore.js @@ -36,7 +36,7 @@ const boyerMoore = (str, pattern) => { // found at this index return offset } - scanIndex++ + scanIndex++ } const badMatchString = str[offset + patternLastIndex] if (badMatchTable[badMatchString]) { From 508f38d379f968df4a72dfd9e2994ff4fca0e1b4 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 21 Apr 2022 22:45:51 +0000 Subject: [PATCH 08/11] Auto-update DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index c9b08760af..db5f619802 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -295,6 +295,7 @@ ## String * [AlphaNumericPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/AlphaNumericPalindrome.js) * [AlternativeStringArrange](https://github.com/TheAlgorithms/Javascript/blob/master/String/AlternativeStringArrange.js) + * [BoyerMoore](https://github.com/TheAlgorithms/Javascript/blob/master/String/BoyerMoore.js) * [CheckAnagram](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckAnagram.js) * [CheckCamelCase](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckCamelCase.js) * [CheckExceeding](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckExceeding.js) From 40bd41c3be55e44b8cd1d704aa56913bce24c254 Mon Sep 17 00:00:00 2001 From: Ayoade David Date: Fri, 22 Apr 2022 00:00:22 +0100 Subject: [PATCH 09/11] Update BoyerMoore.js | Lgtm alert fix. --- String/BoyerMoore.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/String/BoyerMoore.js b/String/BoyerMoore.js index e25a152a1d..25e35233ae 100644 --- a/String/BoyerMoore.js +++ b/String/BoyerMoore.js @@ -26,11 +26,10 @@ const boyerMoore = (str, pattern) => { const badMatchTable = buildBadMatchTable(pattern) let offset = 0 const patternLastIndex = pattern.length - 1 - let scanIndex = patternLastIndex const maxOffset = str.length - pattern.length // if the offset is bigger than maxOffset, cannot be found while (offset <= maxOffset) { - scanIndex = 0 + let scanIndex = 0 while (pattern[scanIndex] === str[scanIndex + offset]) { if (scanIndex === patternLastIndex) { // found at this index From 9181bdef5c1771bc8335d75e29223b3ceb3691c4 Mon Sep 17 00:00:00 2001 From: Ayoade David Date: Sat, 23 Apr 2022 00:23:16 +0100 Subject: [PATCH 10/11] BoyerMoore.js| Final fix indentation. --- String/BoyerMoore.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/String/BoyerMoore.js b/String/BoyerMoore.js index 25e35233ae..2a58ad19d5 100644 --- a/String/BoyerMoore.js +++ b/String/BoyerMoore.js @@ -1,10 +1,9 @@ /* * * - *Implementation of the Boyer-Moore String Search Algo. + *Implementation of the Boyer-Moore String Search Algorithm. *The Boyer–Moore string search algorithm allows linear time in - * search by skipping - * indices when searching inside a string for a pattern. + *search by skipping indices when searching inside a string for a pattern. * * * @@ -39,10 +38,10 @@ const boyerMoore = (str, pattern) => { } const badMatchString = str[offset + patternLastIndex] if (badMatchTable[badMatchString]) { - // increase the offset if it exists + // increase the offset if it exists offset += badMatchTable[badMatchString] } else { - offset += 1 + offset ++ } } return -1 From 1992b41860cbe1d7b8c529438d67ae93e389fbee Mon Sep 17 00:00:00 2001 From: Ayoade David Date: Sat, 23 Apr 2022 10:19:23 +0100 Subject: [PATCH 11/11] BoyerMoore.js| Final Fix :( Hopefully! 12 commits to add an algorithm. --- String/BoyerMoore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/BoyerMoore.js b/String/BoyerMoore.js index 2a58ad19d5..6ec8108b71 100644 --- a/String/BoyerMoore.js +++ b/String/BoyerMoore.js @@ -41,7 +41,7 @@ const boyerMoore = (str, pattern) => { // increase the offset if it exists offset += badMatchTable[badMatchString] } else { - offset ++ + offset++ } } return -1