-
Notifications
You must be signed in to change notification settings - Fork 0
/
680.验证回文串-ii.ts
73 lines (72 loc) · 1.29 KB
/
680.验证回文串-ii.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* @lc app=leetcode.cn id=680 lang=typescript
*
* [680] 验证回文串 II
*
* https://leetcode.cn/problems/valid-palindrome-ii/description/
*
* algorithms
* Easy (40.00%)
* Likes: 575
* Dislikes: 0
* Total Accepted: 131.8K
* Total Submissions: 329.5K
* Testcase Example: '"aba"'
*
* 给你一个字符串 s,最多 可以从中删除一个字符。
*
* 请你判断 s 是否能成为回文字符串:如果能,返回 true ;否则,返回 false 。
*
*
*
* 示例 1:
*
*
* 输入:s = "aba"
* 输出:true
*
*
* 示例 2:
*
*
* 输入:s = "abca"
* 输出:true
* 解释:你可以删除字符 'c' 。
*
*
* 示例 3:
*
*
* 输入:s = "abc"
* 输出:false
*
*
*
* 提示:
*
*
* 1 <= s.length <= 10^5
* s 由小写英文字母组成
*
*
*/
// @lc code=start
function validPalindrome(s: string): boolean {
const fn = (fixFlag: boolean, left: number, right: number): boolean => {
while (left < right) {
if (s[left] !== s[right]) {
if (fixFlag) {
if (fn(false, left + 1, right)) return true;
return fn(false, left, right - 1);
} else {
return false;
}
}
left += 1;
right -= 1;
}
return true;
};
return fn(true, 0, s.length - 1);
}
// @lc code=end