Skip to content

Commit 87010d2

Browse files
committed
Add solution #680
1 parent 130bd08 commit 87010d2

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
645|[Set Mismatch](./0645-set-mismatch.js)|Medium|
167167
648|[Replace Words](./0648-replace-words.js)|Medium|
168168
653|[Two Sum IV - Input is a BST](./0653-two-sum-iv-input-is-a-bst.js)|Easy|
169+
680|[Valid Palindrome II](./0680-valid-palindrome-ii.js)|Easy|
169170
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
170171
695|[Max Area of Island](./0695-max-area-of-island.js)|Medium|
171172
700|[Search in a Binary Search Tree](./0700-search-in-a-binary-search-tree.js)|Easy|

solutions/0680-valid-palindrome-ii.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 680. Valid Palindrome II
3+
* https://leetcode.com/problems/valid-palindrome-ii/
4+
* Difficulty: Easy
5+
*
6+
* Given a string s, return true if the s can be palindrome after deleting at most one
7+
* character from it.
8+
*/
9+
10+
/**
11+
* @param {string} s
12+
* @return {boolean}
13+
*/
14+
var validPalindrome = function(s) {
15+
for (let left = 0, right = s.length - 1; left < right; left++, right--) {
16+
if (s[left] !== s[right]) {
17+
return isPalindrome(s, left + 1, right) || isPalindrome(s, left, right - 1);
18+
}
19+
}
20+
21+
return true;
22+
};
23+
24+
function isPalindrome(s, left, right) {
25+
while (left < right) {
26+
if (s[left] !== s[right]) {
27+
return false;
28+
}
29+
right--;
30+
left++;
31+
}
32+
33+
return true;
34+
}

0 commit comments

Comments
 (0)