From 98e5b370c1e892975609ebc20cc920e0e5017a60 Mon Sep 17 00:00:00 2001 From: R2bEEaton Date: Sat, 1 Jun 2024 15:08:09 -0400 Subject: [PATCH 1/5] Valid palindrome --- String/valid-palindrome.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 String/valid-palindrome.js diff --git a/String/valid-palindrome.js b/String/valid-palindrome.js new file mode 100644 index 0000000..b19a6cd --- /dev/null +++ b/String/valid-palindrome.js @@ -0,0 +1,11 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isPalindrome = function(s) { + s = s.replaceAll(/[^A-Za-z0-9]/g, ""); + for (let i = 0; i < s.length / 2; i++) { + if (s.charAt(i).toLowerCase() !== s.charAt(s.length - 1 - i).toLowerCase()) return false; + } + return true; +}; \ No newline at end of file From e5efda91db4ed376878a6e4347a67bedc2f73c1d Mon Sep 17 00:00:00 2001 From: R2bEEaton Date: Sat, 1 Jun 2024 15:21:07 -0400 Subject: [PATCH 2/5] Valid anagram, maybe I'll do a better version later --- String/valid-anagram.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 String/valid-anagram.js diff --git a/String/valid-anagram.js b/String/valid-anagram.js new file mode 100644 index 0000000..df50bd8 --- /dev/null +++ b/String/valid-anagram.js @@ -0,0 +1,9 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function(s, t) { + // If I felt like it I would make a dictionary counter of the characters and check if those matched + return s.split("").sort().join("") === t.split("").sort().join(""); +}; \ No newline at end of file From 8463dd36cebb471d6a7c5713cd33a41ef16d8525 Mon Sep 17 00:00:00 2001 From: R2bEEaton Date: Sun, 2 Jun 2024 20:41:55 -0400 Subject: [PATCH 3/5] improved! 99th percentile :) --- String/valid-anagram.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/String/valid-anagram.js b/String/valid-anagram.js index df50bd8..870668e 100644 --- a/String/valid-anagram.js +++ b/String/valid-anagram.js @@ -4,6 +4,11 @@ * @return {boolean} */ var isAnagram = function(s, t) { - // If I felt like it I would make a dictionary counter of the characters and check if those matched - return s.split("").sort().join("") === t.split("").sort().join(""); + let d = {}; + s.split("").map((a) => d[a] = a in d ? d[a] + 1 : 1); + t.split("").map((a) => d[a] = a in d ? d[a] - 1 : -1); + for (let a in d) { + if (d[a] !== 0) return false; + } + return true; }; \ No newline at end of file From a0bbaa0daf8e07dc859fcacf642ede6f7db4aa0c Mon Sep 17 00:00:00 2001 From: R2bEEaton Date: Sun, 2 Jun 2024 20:54:40 -0400 Subject: [PATCH 4/5] rotate-image.js --- Matrix/rotate-image/rotate-image.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Matrix/rotate-image/rotate-image.js diff --git a/Matrix/rotate-image/rotate-image.js b/Matrix/rotate-image/rotate-image.js new file mode 100644 index 0000000..9068036 --- /dev/null +++ b/Matrix/rotate-image/rotate-image.js @@ -0,0 +1,20 @@ +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var rotate = function(matrix) { + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j <= i; j++) { + let temp = matrix[j][i]; + matrix[j][i] = matrix[i][j]; + matrix[i][j] = temp; + } + } + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j < matrix.length / 2; j++) { + let temp = matrix[i][j]; + matrix[i][j] = matrix[i][matrix.length - 1 - j]; + matrix[i][matrix.length - 1 - j] = temp; + } + } +}; \ No newline at end of file From 74f998e8a5ba6ab1cfcf46192977c970bcdfec14 Mon Sep 17 00:00:00 2001 From: R2bEEaton Date: Sun, 2 Jun 2024 21:12:26 -0400 Subject: [PATCH 5/5] contains-duplicate.js --- Array/contains-duplicate.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Array/contains-duplicate.js diff --git a/Array/contains-duplicate.js b/Array/contains-duplicate.js new file mode 100644 index 0000000..7708011 --- /dev/null +++ b/Array/contains-duplicate.js @@ -0,0 +1,13 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function(nums) { + // Pretty much a Set but probably with overhead + let d = {}; + for (let i in nums) { + if (nums[i] in d) return true; + d[nums[i]] = 1; + } + return false; +}; \ No newline at end of file