From 8ee2543131dcbb61607c65646e9b60e495334a5d Mon Sep 17 00:00:00 2001 From: Ala Eddine Date: Tue, 9 May 2023 22:07:03 +0100 Subject: [PATCH 1/2] :cyclone: Implement the brute force solution --- src/array/two-sum.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/array/two-sum.js b/src/array/two-sum.js index 5ecac6f..6346649 100644 --- a/src/array/two-sum.js +++ b/src/array/two-sum.js @@ -34,4 +34,29 @@ const twoSum = (nums, target) => { return null; }; +/** + * Brute Force + */ + +/** + * + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +/** + * + */ +const twoSumBrute = function (nums, target) { + for (let i = 0; i < nums.length - 1; i++) { + for (let nextIndex = i + 1; nextIndex < nums.length; nextIndex++) { + if (target === nums[i] + nums[nextIndex]) { + return [i, nextIndex]; + } + } + } +}; + +console.log(twoSumBrute([3, 2, 4], 6)); // [0,1] + export { twoSum }; From 1cdb455aac575305e81a39df259d0656a69e5c5a Mon Sep 17 00:00:00 2001 From: Ala Eddine Date: Tue, 9 May 2023 22:16:16 +0100 Subject: [PATCH 2/2] :cyclone: Use RegEx --- src/string/valid-palindrome-ii.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/string/valid-palindrome-ii.js b/src/string/valid-palindrome-ii.js index 9add85a..6450585 100644 --- a/src/string/valid-palindrome-ii.js +++ b/src/string/valid-palindrome-ii.js @@ -44,4 +44,19 @@ const isPalindromic = (s, i, j) => { return true; }; +/** + * Using RegExp + */ + +var isPalindrome = function(s) { + s = s.replace(/[^0-9a-z]/gi, ''); + return ( + s + .split('') + .reverse() + .join('') + .toLowerCase() == s.toLowerCase() + ); +}; + export { validPalindrome };