Mark As Completed Discussion

Let's move on as we haven't completed our solution yet. Now the problem is that the input string is neither less than 2 nor equal to 2. The only possibility left is that it will be greater than 2. So the question arises, how are we going to deal with such strings?

In this case, we will find all possible substrings with the help of loops. Here i is the starting point of a substring and j is the ending point.

Continuing To Implement

The loops will form the substrings with indices (0, 4), (0, 3), (0, 2), and so on.

The next step is to check if our substring is a palindrome or not. We will check it using the following line of code.

Continuing To Implement

In the following step, if the substring is a palindrome, we will check if it is the longest palindromic substring. For that purpose, we can create an output variable that will store the longest palindromic substring found. Let's then compare our substring with the output string. If it is longer than the output string, we'll update it.

Continuing To Implement

In this way, our loop will iterate through the length of the string and we will find the longest palindromic substring.

If no palindromic substring is found, then output will remain empty and the function will return the first character of the string.

Continuing To Implement