I have a string consisting zeros and ones and I need to know how many deletions from the beginning I need to do in order to make a string a palindrome. I wrote a function to check if the string is palindrome (and iterate over each element) and it works, but I need complexity better than O(n^2). It will be probably O(n) with KMP algorithm but I have no idea how to use it in this case. Can you help me?
Asked
Active
Viewed 114 times
2
-
As fascinating as I find this problem, I'm not convinced that SO is the best place for it. Usually questions on Stack Overflow are focused on _specific programming questions_. There are, no doubt, a variety of solutions to this problem and the _best_ one will often be open to opinion. – Martin Apr 29 '21 at 14:45
-
I don't need the best one. I need a better one (in terms of time complexity). – Ignos Apr 29 '21 at 15:16
-
@Ignos You are right. This can indeed be solved using KMP. Reverse the string and run KMP. Then the farthest index having value = index/2 will be the length of longest palindrome. – risingStark Apr 29 '21 at 15:21
-
But I don't know how since the pattern in changing when I delete chars. – Ignos Apr 29 '21 at 15:22