I have always wanted to learn more about Data Structures and Algorithms.
However, owing to the non-CS background of my graduation, I never had the time to do so.
I thought, I would give it a try and started solving LeetCode problems on 20-September-2024.
As soon as I logged in, the first problem that was thrown at me was the Shortest Palindrome Problem.
Problem description is as follows:
You are given a string
s
. You can converts
to a palindrome by adding characters in front of it.Return the shortest palindrome you can find by performing this
transformation.Example 1:
Input: s = “aacecaaa”
Output: “aaacecaaa”Example 2:
Input: s = “abcd”
Output: “dcbabcd”Constraints:
0 <= s.length <= 5 * 104
s
consists of lowercase English letters only.
I went ahead and tried solving the problem from a pseudocode POV:
- Check the length of
s
to see that the constraint is satisfied - If
len(s)=0
, return result ass
- Start searching
s
and eleminating each element from the back with each iteration, that means check if eachs[0:n]
ors[0:(n-1)]
ors[0:(n-2)]
and so on is a palindrome. - We assume, for now, that we are given a helper function
isPalindrome
, which compares the0th
element with thenth
element and(0+1)th
element with the(n-1)th
element. - Find the shortest possible palindrome -
sp
. - Then reverse the remaining part of the string and add it to the start of
sp
.
Now, if we see the above in action, it will look pretty messy, however, we go ahead and check it anyway:
- Assume we have string :
s=abcd
. - If
len(s) = 0, return s
- Else, run around
s
, check, ifs[0] = s[len(s)-1] = FALSE
:s[0] = s[len(s)-2] = FALSE
:s[0] = s[len(s)-3] = FALSE
:s[0] = s[len(s)-4] = TRUE
:
isPalindrome[s[0]]=TRUE
s' = reverse[s[1:n]]
:concatenate [s', s]
:
I solved it using this, however, you will notice that there are 2 linear loops running,
- loop for checking through each element :
isPalindrome
loop :
The complexity of the above will be .
I was happy :) and submitted, after submission, I saw that there are multiple other algorithmic approaches that give a much better and faster solution to this solution.
This is when I came across the Knuth Morris Pratt Algorithm.
I tried to understand this from a Mathematician’s perspective before getting into the programming.
Often, the computer scientists over simplify a mathematical method and call it an algorithm, this is precisely why, a solution that would have taken an afternoon for a CS grad, is taking more than 2 days for a Mathematician.
Knuth Morris Pratt is an efficient algorithm for substring search 1.
- Prefix Function Definition:
You are given a string s of length n. The prefix function for this string is defined as an array of length n, where is the length of the longest proper prefix of the substring which is also a suffix of this substring. A proper prefix of a string is a prefix that is not equal to the string itself. By definition, .
Mathematically the definition of the prefix function can be written as follows: