Skip to main content
Improved by Shaggy
Source Link
Fhuvi
  • 2.3k
  • 1
  • 8
  • 24

JavaScript (Node.js), 9075 90 bytes

-15 bytes thanks to @Shaggy 's conversion into a recursive function, simplification of the array comparison, and replacement of a map with a reduce.

JS is clearlymight not be the right tool for this job, but i'mi was surprised no one posted a JS answer before!

I'm not sure if this is the shortest approach, but this is the best i could come up with :).

For each character of the word, we test if the removal of this character gives a word that is a palindrome, and we retain the index +1 of the last character that succeeded (or -1 if there weren't any).

s=>[...s].map(f=(es,ir=i=-1)=>r==>s[++i]?f(s,(a=[...s]).splice(i,1)&&a.join``==a&&a+``==a.reverse().join``?i+1:r,r=-1)&&r:r 

Try it online!Try it online!

JavaScript (Node.js), 90 bytes

JS is clearly not the right tool for this job, but i'm surprised no one posted a JS answer before!

I'm not sure if this is the shortest approach, but this is the best i could come up with :)

For each character of the word, we test if the removal of this character gives a word that is a palindrome, and we retain the index +1 of the last character that succeeded (or -1 if there weren't any).

s=>[...s].map((e,i)=>r=(a=[...s]).splice(i,1)&&a.join``==a.reverse().join``?i+1:r,r=-1)&&r 

Try it online!

JavaScript (Node.js), 75 90 bytes

-15 bytes thanks to @Shaggy 's conversion into a recursive function, simplification of the array comparison, and replacement of a map with a reduce.

JS might not be the right tool for this job, but i was surprised no one posted a JS answer before.

For each character of the word, we test if the removal of this character gives a word that is a palindrome, and we retain the index +1 of the last character that succeeded (or -1 if there weren't any).

f=(s,r=i=-1)=>s[++i]?f(s,(a=[...s]).splice(i,1)&&a+``==a.reverse()?i+1:r):r 

Try it online!

Source Link
Fhuvi
  • 2.3k
  • 1
  • 8
  • 24

JavaScript (Node.js), 90 bytes

JS is clearly not the right tool for this job, but i'm surprised no one posted a JS answer before!

I'm not sure if this is the shortest approach, but this is the best i could come up with :)

For each character of the word, we test if the removal of this character gives a word that is a palindrome, and we retain the index +1 of the last character that succeeded (or -1 if there weren't any).

s=>[...s].map((e,i)=>r=(a=[...s]).splice(i,1)&&a.join``==a.reverse().join``?i+1:r,r=-1)&&r 

Try it online!