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.
Below is my C# solution with explaination in comments. Hope you find it useful.
publicstaticintpalindromeIndex(strings){for(inti=0,j=s.Length-1;i<j;i++,j--){if(s[i]!=s[j]){// remove 1 char at i or j// then check for condition// if false -> do not need to check the remain positions// because it can not be palindrome anymore// return -1if(CheckPalindrome(s,i+1,j))returni;//remove at position i & checkif(CheckPalindrome(s,i,j-1))returnj;//remove at position j & checkreturn-1;// there is no solution}}return-1;// it's palindrome already }publicstaticboolCheckPalindrome(strings,intleft,intright){for(inti=left,j=right;i<j;i++,j--){if(s[i]!=s[j])returnfalse;}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 →
Below is my C# solution with explaination in comments. Hope you find it useful.