-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Create 0680-valid-palindrome-II.js #1581
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
Conversation
Solution for Valid Palindrome II in JS.
var validPalindromeBrute = function(s) { | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
if (isPalindrome(s.slice(0, i) + s.slice(i + 1))) return true; |
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.
Create a function isValid and invoke it
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, It's done.
renamed isPalindrome to isValid.
@aakhtar3, I renamed the function. Please check. |
var validPalindromeBrute = function(s) { | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
if (isValid(s.slice(0, i) + s.slice(i + 1))) return true; |
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 should invoke the logic in a helper function, not in the if condition
if (isValidPalindrome(...)) return true
if (s[left] !== s[right]) { | ||
|
||
if ( | ||
isValid(s.slice(left + 1, right + 1)) || |
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.
Same here
* @return {boolean} | ||
*/ | ||
// | ||
var validPalindromeBrute = function(s) { |
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.
Since Java script hoist vars, you can have the same name
Added logic inside the helper function. Removed the brute force solution.
Hey @aakhtar3, I removed the brute force approach and added the logic inside the helper function. Please check. |
@@ -0,0 +1,35 @@ | |||
/** | |||
* Linear Time. | |||
* Time O(N) | Space O(1) |
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.
Time is N^2
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.
Space is O(N)
|
||
function isValid(s, left, right) { | ||
s = s.slice(left, right); | ||
return s.split('').reverse().join('') === s; |
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 chain with slice
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.
Ok, But I think that will be too much chaining. People might not understand that. I updated the code.
Updated the Valid-Palindrome-ii.js
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
// |
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.
Remove
while (left < right) { | ||
if (s[left] !== s[right]) { | ||
|
||
if ( |
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 can be cleaned up a bit more
const isValid = isPalindrome(left, right) || isPalindrome(left + 1, right + 1)
if (isValid) return true;
const isPalindrome = (s, left, right) => {
const s = s.slice(left, right)
return s === reOrder(s);
}
const reOrder = (s) => s
.split('')
.reverse()
.join('')
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.
Hey @aakhtar3, We can't reassign s in isPalindrome as this is already coming as a parameter. You forgot to pass the first argument in isPalindrome on the very first line. I did the suggested changes. Please check now.
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.
Oh, sorry I meant you can't re-create the variable by the same name in the isPalindrome.
I update the code with suggested changes. Here's the submission link for the problem -> https://leetcode.com/problems/valid-palindrome-ii/submissions/865395795/
while (left < right) { | ||
if (s[left] !== s[right]) { | ||
const isValid = isPalindrome(s, left, right) || isPalindrome(s, left + 1, right + 1); | ||
if (isValid) return true; |
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 remove if else and just return isValid
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.
That is a great catch!! I'm so stupid. That's great, why didn't I think of that? Thanks, @aakhtar3. I updated the code please check.
Returning isValid instead of checking with the if statement.
@aadil42 rename the file to 0680-valid-palindrome-ii.js to follow Contributing guidelines naming standards. Additionally, the time complexity is not O(n) since we are performing palindrome inside of a while loop, the time complexity would be O(N^2). Space Complexity would be O(n) since a new string is being constructed with the reOrder function. |
@Ykhan799, yes you're right. Good catch. I have updated the time and space complexity and renamed the file. Please review. |
@aadil42 for the file name Valid-Palindrome should all be in lowercase to match the naming standards of all other files. Once you fix this, I'll merge this PR. |
@Ykhan799, someone already contributed the solution for this problem: https://github.com/neetcode-gh/leetcode/blob/main/javascript/0680-valid-palindrome-ii.js I am closing the PR. |
The solution is added by someone else and this solution is almost identical to it. |
Solution for Valid Palindrome II in JS.
File(s) Added: 0680-Valid-Palindrome-II.js
Language(s) Used: JavaScript
Submission URL: https://leetcode.com/problems/valid-palindrome-ii/submissions/863970166/