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.
publicstaticinttheLoveLetterMystery(Strings){//goal: find min operations req to convert string s into a palindrome//o(n) time, o(1) spaceif(s.length()==1)return0;//len of 1 is always palindromicintminOperations=0;intleft=0,right=s.length()-1;//use a left pointer at the start and a right pointer at the end to process both ends of strwhile(left<right){charleftChar=s.charAt(left);charrightChar=s.charAt(right);//if char at left is < right, reduce rightif(leftChar<rightChar){minOperations+=rightChar-leftChar;}//if char at left is > right, reduce leftelseif(leftChar>rightChar){minOperations+=leftChar-rightChar;}left++;right--;}returnminOperations;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Love-Letter Mystery
You are viewing a single comment's thread. Return to all comments →
My Java solution: