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.
intpalindromeIndex(strings){size_tleft{},right{s.size()-1};// last occurrence std::pair<int,int>index{};intcursor{-1};// track direction pointer is moving in case of char mismatch// 0: left++// 1: right--// 2: no solution found left-right size_tcounter{};while(left<right){if(s[left]==s[right]){left++;right--;}else{if(counter>=2)return-1;elseif(counter==1){left=index.first;right=index.second;cursor=right;right--;}else{index={left,right};cursor=left;left++;}counter++;}}returncursor;}
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 →
O(n) c++ solution: