Instead of finding the ending index from the beginning , find the index from the end This is because if the current sub - string is a palindrome then there is no need to check the sub - strings of smaller length and we can skip to the next iteration of the outer loop
for j in range ( n - 1 , i + 1 , - 1 ) : NEW_LINE