From 3ca1f9926a23bc0a7a248e2e05f9a06993d691f7 Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Thu, 22 Jun 2023 19:04:37 +0530 Subject: [PATCH 1/5] Create 1898-maximum-number-of-removable-characters.js Solved maximum-number-of-removable-characters in JS. --- ...-maximum-number-of-removable-characters.js | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 javascript/1898-maximum-number-of-removable-characters.js diff --git a/javascript/1898-maximum-number-of-removable-characters.js b/javascript/1898-maximum-number-of-removable-characters.js new file mode 100644 index 000000000..23185c6b3 --- /dev/null +++ b/javascript/1898-maximum-number-of-removable-characters.js @@ -0,0 +1,89 @@ +/** + * https://leetcode.com/problems/maximum-number-of-removable-characters/ + * + * Brute force + * Time O(removable.length * s.length) | Space O(1) + * @param {string} s + * @param {string} p + * @param {number[]} removable + * @return {number} + */ +var maximumRemovals1 = function(s, p, removable) { + + let k = 0; + // removable.reverse(); + s = s.split(''); + p = p.split(''); + for(let i = 0; i < removable.length; i++) { + s[removable[i]] = -1; + if(isSubSet()) { + k++; + } else { + return k; + } + } + + function isSubSet() { + let i = 0; + let j = 0; + + while(i < s.length && j < p.length) { + if(s[i] === p[j]) { + i++; + j++; + } else { + i++; + } + } + return j === p.length + } + + return k; +}; + + +/** + * + * Binary Search + * n = length of string, k = length of removable + * Time O(log(k)*n) | Space O(k) + * @param {string} s + * @param {string} p + * @param {number[]} removable + * @return {number} + */ +var maximumRemovals = function(s, p, removable) { + + s = s.split(''); + p = p.split(''); + let left = 0; + let right = removable.length - 1; + let k = 0; + while(left <= right) { + const mid = Math.floor((left+right)/2); + const hash = new Set(removable.slice(0, mid + 1)); + if(isSubSet(hash)) { + k = Math.max(k, mid + 1); + left = mid + 1; + } else { + right = mid - 1; + } + } + + function isSubSet(hash) { + let i = 0; + let j = 0; + + while(i < s.length && j < p.length) { + if(s[i] === p[j] && !hash.has(i)) { + i++; + j++; + } else { + i++; + } + } + return j === p.length + } + + return k; +}; From 6bafb1f104186b524e6d92eadf6b6f3def6c0e43 Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Tue, 4 Jul 2023 20:59:44 +0530 Subject: [PATCH 2/5] Update 1898-maximum-number-of-removable-characters.js Avoiding else case and defining function outside the main solution function. --- ...-maximum-number-of-removable-characters.js | 113 ++++++++++-------- 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/javascript/1898-maximum-number-of-removable-characters.js b/javascript/1898-maximum-number-of-removable-characters.js index 23185c6b3..fdce8cf21 100644 --- a/javascript/1898-maximum-number-of-removable-characters.js +++ b/javascript/1898-maximum-number-of-removable-characters.js @@ -9,37 +9,39 @@ * @return {number} */ var maximumRemovals1 = function(s, p, removable) { + let k = 0; + // removable.reverse(); + s = s.split(''); + p = p.split(''); - let k = 0; - // removable.reverse(); - s = s.split(''); - p = p.split(''); - for(let i = 0; i < removable.length; i++) { - s[removable[i]] = -1; - if(isSubSet()) { - k++; - } else { - return k; - } + for (let i = 0; i < removable.length; i++) { + s[removable[i]] = -1; + if (isSubSet1(s, p)) { + k++; + } else { + return k; } + } + + return k; +}; - function isSubSet() { - let i = 0; - let j = 0; +// helper function. +function isSubSet1(s, p) { + let i = 0; + let j = 0; - while(i < s.length && j < p.length) { - if(s[i] === p[j]) { - i++; - j++; - } else { - i++; - } - } - return j === p.length + while (i < s.length && j < p.length) { + if (s[i] === p[j]) { + i++; + j++; + } else { + i++; + } } - return k; -}; + return j === p.length; +} /** @@ -53,37 +55,42 @@ var maximumRemovals1 = function(s, p, removable) { * @return {number} */ var maximumRemovals = function(s, p, removable) { + s = s.split(''); + p = p.split(''); + let left = 0; + let right = removable.length - 1; + let k = 0; - s = s.split(''); - p = p.split(''); - let left = 0; - let right = removable.length - 1; - let k = 0; - while(left <= right) { - const mid = Math.floor((left+right)/2); - const hash = new Set(removable.slice(0, mid + 1)); - if(isSubSet(hash)) { - k = Math.max(k, mid + 1); - left = mid + 1; - } else { - right = mid - 1; - } - } + while (left <= right) { + const mid = Math.floor((left + right) / 2); + const hash = new Set(removable.slice(0, mid + 1)); - function isSubSet(hash) { - let i = 0; - let j = 0; + if (isSubSet(hash, s, p)) { + k = Math.max(k, mid + 1); + left = mid + 1; + continue; + } - while(i < s.length && j < p.length) { - if(s[i] === p[j] && !hash.has(i)) { - i++; - j++; - } else { - i++; - } - } - return j === p.length + right = mid - 1; } - return k; + return k; }; + +// helper function. +function isSubSet(hash, s, p) { + let i = 0; + let j = 0; + + while (i < s.length && j < p.length) { + if (s[i] === p[j] && !hash.has(i)) { + i++; + j++; + continue; + } + + i++; + } + + return j === p.length; +} From 03154cadd072c506dd7ce5ad0acd95d2e6e87d02 Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Sat, 22 Jul 2023 15:44:43 +0530 Subject: [PATCH 3/5] Update 1898-maximum-number-of-removable-characters.js Updating the space complexity for the brute force approach. --- javascript/1898-maximum-number-of-removable-characters.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascript/1898-maximum-number-of-removable-characters.js b/javascript/1898-maximum-number-of-removable-characters.js index fdce8cf21..3d50591da 100644 --- a/javascript/1898-maximum-number-of-removable-characters.js +++ b/javascript/1898-maximum-number-of-removable-characters.js @@ -2,7 +2,7 @@ * https://leetcode.com/problems/maximum-number-of-removable-characters/ * * Brute force - * Time O(removable.length * s.length) | Space O(1) + * Time O(removable.length * s.length) | Space O(n) * @param {string} s * @param {string} p * @param {number[]} removable From 6ae6bb121f92b4d824a5717a33d2b3bc75c88122 Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Sat, 22 Jul 2023 15:52:22 +0530 Subject: [PATCH 4/5] Update 1898-maximum-number-of-removable-characters.js Avoiding else case. --- javascript/1898-maximum-number-of-removable-characters.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/javascript/1898-maximum-number-of-removable-characters.js b/javascript/1898-maximum-number-of-removable-characters.js index 3d50591da..e33eaf6e5 100644 --- a/javascript/1898-maximum-number-of-removable-characters.js +++ b/javascript/1898-maximum-number-of-removable-characters.js @@ -18,9 +18,9 @@ var maximumRemovals1 = function(s, p, removable) { s[removable[i]] = -1; if (isSubSet1(s, p)) { k++; - } else { - return k; - } + continue; + } + return k; } return k; From f58bdb22960767cfd599c9791e993ccb9e8cb47d Mon Sep 17 00:00:00 2001 From: aadil42 <77232799+aadil42@users.noreply.github.com> Date: Thu, 7 Sep 2023 12:40:53 +0530 Subject: [PATCH 5/5] Update 1898-maximum-number-of-removable-characters.js Using bitwise operation to get the middle value. Not converting the strings to array to keep the time complexity O(1). --- javascript/1898-maximum-number-of-removable-characters.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/javascript/1898-maximum-number-of-removable-characters.js b/javascript/1898-maximum-number-of-removable-characters.js index e33eaf6e5..3ae59a871 100644 --- a/javascript/1898-maximum-number-of-removable-characters.js +++ b/javascript/1898-maximum-number-of-removable-characters.js @@ -48,21 +48,20 @@ function isSubSet1(s, p) { * * Binary Search * n = length of string, k = length of removable - * Time O(log(k)*n) | Space O(k) + * Time O(log(k)*n) | Space O(1) * @param {string} s * @param {string} p * @param {number[]} removable * @return {number} */ var maximumRemovals = function(s, p, removable) { - s = s.split(''); - p = p.split(''); + let left = 0; let right = removable.length - 1; let k = 0; while (left <= right) { - const mid = Math.floor((left + right) / 2); + const mid = (left + right) >> 1; const hash = new Set(removable.slice(0, mid + 1)); if (isSubSet(hash, s, p)) {