Solved a problem from leetcode. Problem Name:Shortest Palindrome Level:Hard Link to the problem: leetcode.com/p... Recommended: Watch it on 1.75x or 2x.
10:06 Here we should not increment "j". There is a mistake in explanation. Check the code we are not incrementing "j" there in else statement. we are only changing the pointer "i".
class Solution: def shortestPalindrome(self, s: str) -> str: if s == s[::-1]: return s n = len(s) # Loop to find the largest palindrome prefix for i in range(n, 0, -1): if s[:i] == s[:i][::-1]: break # Add the reverse of the suffix to the start of the string suffix = s[i:] return suffix[::-1] + s