We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functionpalindromeIndex(str){// check if the string is already a palindromeif(isPalindrome(str)){return-1;}// for(leti=0;i<Math.floor(str.length/2);i++){letnewStr=str.slice(0,i)+str.slice(i+1);if(isPalindrome(newStr)){returni;}if(str[i]!=str[str.length-1-i]){returnisPalindrome(str.slice(0,str.length-1-i)+str.slice(str.length-i))?str.length-1-i:-1;}}return-1;}// helper function to check if a string is a palindromefunctionisPalindrome(str){letpalindrom=true;leti=0;while(i<Math.floor(str.length/2)&&palindrom){palindrom=str[i]==str[str.length-i-1]i++;}returnpalindrom}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Palindrome Index
You are viewing a single comment's thread. Return to all comments →
Easy Solution with Javascript