How To Solve The Palindrome Index Code Challenge

How To Solve HackerRank’s Palindrome Index Code Challenge With JavaScript

How To Solve HackerRank’s Palindrome Index Code Challenge With JavaScript

Problem

// Example
"ababab"
// Result
5
// Reason, when index 5 is removed
"ababa" = "ababa" the reverse

Write a function or functions that returns the index of the letter removed from string (s) in order to make it a palindrome and -1 is it not possible or no string is removed

Covering Our Bases

function palindromeIndex (s) {
let result = -1;
const slen = s.length;

if (slen >= 1 && slen <= 100005) {
// continue code
}

return result;
}
function palindromeIndex (s) {
let result = -1;
const slen = s.length;

if (slen >= 1 && slen <= 100005 & s !== s.split('').reverse().join('')) {
// continue code
}

return result;
}

Understanding The Problem

Traversing Characters

function palindromeIndex (s) {
let result = -1;
const slen = s.length;

if (slen >= 1 && slen <= 100005 & s !== s.split('').reverse().join('')) {

for (let i = 0; i < slen; i++) {
// continue code
}
}

return result;
}

Creating New Strings

function palindromeIndex (s) {
let result = -1;
const slen = s.length;

if (slen >= 1 && slen <= 100005 & s !== s.split('').reverse().join('')) {

for (let i = 0; i < slen; i++) {

// create new string
let newS = s.substring(0, i) + s.substring((i + 1));

// create the reverse of that string
let revNewS = newS.split('').reverse().join('');

// compare
if (newS === revNewS) {
result = i;
break;
}
}
}

return result;
}

Problems With Our First Solution

Terminated due to timeout

Understanding The Problem Again

Palindrome Requirements

// Example - "acbba"↓       ↓
a c b b a = same
↓ ↓
a c b b a = 🚫not the same

Comparing Letters

function palindromeIndex (s) {
let result = -1;
const slen = s.length;

if (slen >= 1 && slen <= 100005 & s !== s.split('').reverse().join('')) {

for (let i = 0; i < slen; i++) {

// comparing the first letter with the last, etc
if (s.charAt(i) != s.charAt(len - 1 - i)) {
}
}
}

return result;
}

Deciding Which Letter To Remove

// Example with for loop - "acbba"
...
i = 1
↓ ↓
a c b b a = 🚫not the same
// Expected answer should be i (1)// Example with for loop - "abbca"
...
i = 1
↓ ↓
a b b c a = 🚫not the same
// Expected answer should be 3 or (length - 1 - i)
function palindromeIndex (s) {
let result = -1;
const slen = s.length;

if (slen >= 1 && slen <= 100005 & s !== s.split('').reverse().join('')) {

for (let i = 0; i < slen; i++) {

// comparing the first letter with the last, etc
if (s.charAt(i) != s.charAt(slen - 1 - i)) {

// accounting for the letter at the beg.
let s1 = s.substring(0, i) + s.substring((i + 1));
// accounting for the letter at the end
let s2 = s.substring(0, (slen - 1 - i)) + s.substring((len - 1 - i) + 1);
}
}
}

return result;
}

Comparing The Strings

function palindromeIndex (s) {
let result = -1;
const slen = s.length;

if (slen >= 1 && slen <= 100005 & s !== s.split('').reverse().join('')) {

for (let i = 0; i < slen; i++) {

// comparing the first letter with the last, etc
if (s.charAt(i) != s.charAt(slen - 1 - i)) {

// accounting for the letter at the beg.
let s1 = s.substring(0, i) + s.substring((i + 1));
// accounting for the letter at the end
let s2 = s.substring(0, (slen - 1 - i)) + s.substring((len - 1 - i) + 1);

if (s1 === s1.split('').reverse().join('')) {
result = i;
break;
} else if (s2 === s2.split('').reverse().join('')) {
result = slen - 1 - i;
break;
}
}
}
}

return result;
}

What About More Than One Letter

// Example scenario - "acbdbba"... 
i = 1
↓ ↓
a c b d b b a = 🚫not the same
// if we removed c
"abdbba" != "abbdba"
// if we removed b
"acbdba" != "abdbca"
// for it to be a palindrome we would need to remove another letter
// which is out of our requirements, hence result = -1
function palindromeIndex (s) {
let result = -1;
const slen = s.length;

if (slen >= 1 && slen <= 100005 & s !== s.split('').reverse().join('')) {

for (let i = 0; i < slen; i++) {

// comparing the first letter with the last, etc
if (s.charAt(i) != s.charAt(slen - 1 - i)) {

// accounting for the letter at the beg.
let s1 = s.substring(0, i) + s.substring((i + 1));
// accounting for the letter at the end
let s2 = s.substring(0, (slen - 1 - i)) + s.substring((len - 1 - i) + 1);

if (s1 === s1.split('').reverse().join('')) {
result = i;
break;
} else if (s2 === s2.split('').reverse().join('')) {
result = slen - 1 - i;
break;
} else {
// result is already -1 from the top
break;
}
}
}
}

return result;
}

Optimizing Code Even More

function palindromeIndex (s) {
let result = -1;
const slen = s.length;

if (slen >= 1 && slen <= 100005 & s !== s.split('').reverse().join('')) {

for (let i = 0; i < slen; i++) {

// comparing the first letter with the last, etc
if (s.charAt(i) != s.charAt(slen - 1 - i)) {

// accounting for the letter at the beg.
let s1 = s.substring(0, i) + s.substring((i + 1));
// accounting for the letter at the end
let s2 = s.substring(0, (slen - 1 - i)) + s.substring((len - 1 - i) + 1);

if (s1 === s1.split('').reverse().join('')) {
result = i;
} else if (s2 === s2.split('').reverse().join('')) {
result = slen - 1 - i;
}

// else is assumed
// and a break is assumed
// also accounts for result = -1
break;

}
}
}

return result;
}
function palindromeIndex (s) {
let result = -1;
const slen = s.length;

if (slen >= 1 && slen <= 100005 & s !== s.split('').reverse().join('')) {

for (let i = 0; i < slen / 2; i++) {

// comparing the first letter with the last, etc
if (s.charAt(i) != s.charAt(slen - 1 - i)) {

// accounting for the letter at the beg.
let s1 = s.substring(0, i) + s.substring((i + 1));
// accounting for the letter at the end
let s2 = s.substring(0, (slen - 1 - i)) + s.substring((len - 1 - i) + 1);

if (s1 === s1.split('').reverse().join('')) {
result = i;
} else if (s2 === s2.split('').reverse().join('')) {
result = slen - 1 - i;
}
// else is assumed
// and a break is assumed
// also accounts for result = -1
break;
}
}
}

return result;
}

Solution

function palindromeIndex (s) {
let result = -1;
const slen = s.length;

if (slen >= 1 && slen <= 100005 & s !== s.split('').reverse().join('')) {
for (let i = 0; i < slen; i++) {
if (s.charAt(i) != s.charAt(slen - 1 - i)) {
let s1 = s.substring(0, i) + s.substring((i + 1));
let s2 = s.substring(0, (slen - 1 - i)) + s.substring((len - 1 - i) + 1);

if (s1 === s1.split('').reverse().join('')) {
result = i;
} else if (s2 === s2.split('').reverse().join('')) {
result = slen - 1 - i;
}
break;
}
}
}

return result;
}

Test Cases

// "a", Expected -1
// "", Expected -1
// "acbdbba", Expected -1
// "aaab", Expected 3
// "acbba", Expected 1
palindromeIndex("a"); // -1 ✅
palindromeIndex(""); // -1 ✅
palindromeIndex("acbdbba"); // -1 ✅
palindromeIndex("aaab"); // 3 ✅
palindromeIndex("acbba"); // 1 ✅

Feedback

Web Application / Full Stack JavaScript Developer & Aspiring DevOps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store