Skip to content

Conversation

aadil42
Copy link
Contributor

@aadil42 aadil42 commented Dec 23, 2022

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/

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;
Copy link
Collaborator

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

Copy link
Contributor Author

@aadil42 aadil42 Dec 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aakhtar3, It's done.

@aadil42
Copy link
Contributor Author

aadil42 commented Dec 24, 2022

@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;
Copy link
Collaborator

@aakhtar3 aakhtar3 Dec 24, 2022

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)) ||
Copy link
Collaborator

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) {
Copy link
Collaborator

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.
@aadil42
Copy link
Contributor Author

aadil42 commented Dec 24, 2022

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time is N^2

Copy link
Collaborator

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;
Copy link
Collaborator

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

Copy link
Contributor Author

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}
*/
//
Copy link
Collaborator

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 (
Copy link
Collaborator

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('')

Copy link
Contributor Author

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.

Copy link
Contributor Author

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;
Copy link
Collaborator

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

Copy link
Contributor Author

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.
@Ykhan799
Copy link
Collaborator

@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.

@aadil42
Copy link
Contributor Author

aadil42 commented Oct 23, 2024

@Ykhan799, yes you're right. Good catch. I have updated the time and space complexity and renamed the file. Please review.

@aadil42 aadil42 changed the title Create 680-Valid-Palindrome-II.js Create 0680-Valid-Palindrome-II.js Oct 23, 2024
@Ykhan799
Copy link
Collaborator

@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.

@aadil42 aadil42 changed the title Create 0680-Valid-Palindrome-II.js Create 0680-valid-palindrome-II.js Oct 24, 2024
@aadil42
Copy link
Contributor Author

aadil42 commented Oct 24, 2024

@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.

@aadil42 aadil42 closed this Oct 24, 2024
@aadil42
Copy link
Contributor Author

aadil42 commented Oct 24, 2024

The solution is added by someone else and this solution is almost identical to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants