forked from tangweikun/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
23 lines (19 loc) · 638 Bytes
/
index.ts
File metadata and controls
23 lines (19 loc) · 638 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// TODO: Copy from other place, should understand it later
export function validPalindrome(str: string) {
for (let left = 0; left < str.length / 2; left++) {
if (str.charAt(left) !== str.charAt(str.length - 1 - left)) {
let right = str.length - 1 - left
return (
isPalindromeRange(str, left + 1, right) ||
isPalindromeRange(str, left, right - 1)
)
}
}
return true
}
function isPalindromeRange(str: string, left: number, right: number) {
for (let i = left; i <= left + (right - left) / 2; i++) {
if (str.charAt(i) !== str.charAt(right - i + left)) return false
}
return true
}