# longest palindrome substring

Finding the longest substring in a given string that is also a palindrome is known as the longest palindrome substring problem. A word, phrase, number, or other string of letters that reads the same both forward and backward is known as a palindrome. As an illustration, "racecar" and "level" are palindromes.

The longest continuous substring of a given string that is a palindrome is called the "longest palindrome substring." In other words, the longest group of characters inside the original string reads the same forward and backward. The length of this substring can be odd or even.

In computer science, finding the longest palindrome substring within a given string is a frequent problem that is frequently resolved using a variety of methods and strategies.

### Longest Palindromic Substring Naive Approach:

The basic method of determining the lengthiest palindromic substring is creating all possible substrings of the supplied string and determining whether or not each substring is a palindrome for each one. All potential substrings of lengths 1, 2, and so on, up to the length of the input string, can be generated first. We determine if each substring is a palindrome and record the length of the longest palindrome thus far.

The naive technique has an O(n3) time complexity, which makes it inefficient, especially for large strings. It requires O(n2) time to generate all possible substrings and O(n) time to determine whether each substring is a palindrome.

The longest palindrome substring can be found using this method; however, for bigger inputs, it is advised to utilize more effective techniques, such as the dynamic programming method previously described.

### Java Code:

Output:

```"bab" or "aba" (both are possible; one will be printed as output)
```

By comparing characters from the string's beginning and ending up until the point where they meet in the middle, the isPalindrome function determines whether or not a given string, s, is a palindrome.

The largest palindrome naive function uses nested loops to generate every conceivable substring and determines whether each is a palindrome. The longest palindrome thus far is kept track of, and the outcome is returned.

When the code is executed, one of the two substrings, "bab" or "aba" will be printed as the output because they are both acceptable answers for the largest palindromic substring in the input string "babad." Although more effective ways exist to obtain the answer than the naive technique, it should still produce the right results.

### In Python:

Output:

```aaa
```

The program includes functions to determine whether a string is a palindrome and the length of the longest palindromic substring. The is_palindrome function checks a string's palindrome status by comparing it to its reverse. The longest_palindrome_naive function creates all of the input string's substrings and determines whether each is a palindrome while keeping track of the longest one discovered. The function returns the longest palindromic substring. Finding the longest palindromic substring of "baaab," which is the input string itself because it is a palindrome, as demonstrated in the example usage. The code is extremely complex, and better techniques are preferable for longer strings.

## Using Dynamic Programming:

In java:

Output:

```"bab" or "aba" (both are possible)
```

The Java code above implements the dynamic programming method to identify the longest palindromic substring in an input string s. To save time, the method longestPalindromeDP stores whether or not substrings are palindromes in a 2D boolean array called dp. All single characters and all pairs of adjacent characters are initially initialized for dp. Then, larger substrings are iteratively checked, and the array is updated based on whether the current substring is a palindrome. The longest palindromic substring discovered using start and maxLength is recorded by the function. The final output is the substring from start to start + maxLength - 1. The dynamic programming method considerably increases effectiveness and reduces the problem's time complexity to O(n^2).

In python:

Output:

```"aaaa"  (the longest palindromic substring in "baaaac")
```

Finding the longest palindromic substring in the input string "baaaac" is done using dynamic programming by the Python function longest_palindrome_dp. The algorithm initializes a two-dimensional boolean array called dp to hold whether or not substrings are palindromes. It identifies two-character palindromes and flags all substrings with length 1 as palindromes. To determine whether the current substring is a palindrome based on smaller substrings, it repeatedly examines longer ones. The function checks for palindromes and updates the start index and max_length variables to record the largest palindromic substring so far identified. The function returns the longest palindromic substring as start to start + max_length once the dynamic programming table has been finished. It recognizes "a" as the longest palindromic substring in "baaaac" in this particular instance. The dynamic programming method assures that the issue has an effective time complexity of O(n2), making it a preferred solution for longer input strings.