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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more