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.
publicstaticintpalindromeIndex(Strings){//goal: find idx of char that will make str a palindrome when its removedif(s.length()==1)return-1;//already a palindrome//use two pointers to iterate from left and right of strintleft=0,right=s.length()-1;while(left<right){charleftChar=s.charAt(left);charrightChar=s.charAt(right);//if left pointer != right pointer, check which idx will make a palindrome if removedif(leftChar!=rightChar){if(isPalindrome(s,left+1,right))returnleft;elsereturnright;}left++;right--;}return-1;//str is already a palindrome}//uses two pointers to make sure chars are mirroredpublicstaticbooleanisPalindrome(Strings,intleft,intright){while(left<right){if(s.charAt(left)!=s.charAt(right))returnfalse;//chars arent mirroredleft++;right--;}returntrue;}
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 →
My Java solution: