-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create 1898-maximum-number-of-removable-characters.js #2621
Merged
+95
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3ca1f99
Create 1898-maximum-number-of-removable-characters.js
aadil42 6bafb1f
Update 1898-maximum-number-of-removable-characters.js
aadil42 03154ca
Update 1898-maximum-number-of-removable-characters.js
aadil42 6ae6bb1
Update 1898-maximum-number-of-removable-characters.js
aadil42 f58bdb2
Update 1898-maximum-number-of-removable-characters.js
aadil42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* https://leetcode.com/problems/maximum-number-of-removable-characters/ | ||
* | ||
* Brute force | ||
* Time O(removable.length * s.length) | Space O(n) | ||
* @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 (isSubSet1(s, p)) { | ||
k++; | ||
continue; | ||
} | ||
return k; | ||
} | ||
|
||
return k; | ||
}; | ||
|
||
// 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; | ||
} | ||
|
||
|
||
/** | ||
* | ||
* Binary Search | ||
* n = length of string, k = length of removable | ||
* 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) { | ||
|
||
let left = 0; | ||
let right = removable.length - 1; | ||
let k = 0; | ||
|
||
while (left <= right) { | ||
const mid = (left + right) >> 1; | ||
const hash = new Set(removable.slice(0, mid + 1)); | ||
|
||
if (isSubSet(hash, s, p)) { | ||
k = Math.max(k, mid + 1); | ||
left = mid + 1; | ||
continue; | ||
} | ||
|
||
right = mid - 1; | ||
} | ||
|
||
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will increase space complexity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the space complexity to n. But we already had s and p as strings we just used them in an array instead of a string. wouldn't time complexity be the same for that? Before we were using n space for string which we were getting as an argument. Now we use the same number of array elements.
In other words, since we're just converting some string of n char to be an array of n elements. Then, the space complexity is the same as before right? because we were already using space(n) for the strings. Let me know if I'm making a mistake. Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sting is immutable primitive data type. You can not modify its state or space in memory.
If you split and convert to char array, it will need new space to allow the list to exist.
If you need to modify the string you can split, but if you need to read by index, you do not need to split and it will lower space complexity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aakhtar3, I updated the space complexity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can reduce the space O(1) by avoiding the split
and just iterate with
for (const char of str)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aakhtar3, but if we don't convert it to the array then how are we going to update a specific char of the string? See line 18.
I did remove the split method on the binary-search solution and used the bitwise operation to find the mid now. Please check.