Published
Edited
Oct 23, 2019
Insert cell
md`# Leetcode 680. Valid Palindrome II
Given a non-empty string, you may delete at most one character. Judge whether you can make it a palindrome
`
Insert cell
tests = ({
"aba": true,
"abca": true
})
Insert cell
validPalindrome = (s) => {
for(let i = 0, stop = s.length / 2; i < stop; i++) {
let j = s.length - i - 1
if (s[i] !== s[j]) {
return isPalindrome(cut(s, i)) || isPalindrome(cut(s, j))
}
return true
}
}
Insert cell
cut = (s, i) => s.substr(0, i) + s.substr(i + 1)
Insert cell
isPalindrome = s => s === s.split('').reverse().join('')
Insert cell
Object.entries(tests).map(t => {
const isValidPalindrome = validPalindrome(t[0])
const labelResult = isValidPalindrome ? "can" : "can't"
const emojiAssertion = isValidPalindrome === t[1] ? '✅' : '😮'
return `${emojiAssertion} ${t[0]} ${labelResult} be made a palindrome`
})
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more