Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 65691a8

Browse files
author
linjiajian999
committed
String: 680. Valid Palindrome II
1 parent 1f81894 commit 65691a8

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎String/680. Valid Palindrome II.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://leetcode.com/problems/valid-palindrome-ii/description/
2+
/**
3+
* 680. Valid Palindrome II
4+
* @param {string} s
5+
* @return {boolean}
6+
*/
7+
var isEqual = function(s, left, right) {
8+
while (left < right) {
9+
if (s[left] === s[right]) {
10+
left++;
11+
right--;
12+
} else {
13+
return false;
14+
}
15+
}
16+
return true;
17+
}
18+
var validPalindrome = function(s) {
19+
let left = 0;
20+
let right = s.length - 1;
21+
while (left < right) {
22+
if (s[left] != s[right]) {
23+
return isEqual(s, left + 1, right) || isEqual(s, left, right - 1);
24+
} else {
25+
left++;
26+
right--;
27+
}
28+
}
29+
return true;
30+
};
31+
console.log(validPalindrome('abca'));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /