|
| 1 | +# Palindromic Substring |
| 2 | + |
| 3 | +## Palindromic Substring |
| 4 | + |
| 5 | +Given a string, s, return the number of palindromic substrings contained in it. A substring is a contiguous sequence of |
| 6 | +characters in a string. A palindrome is a phrase, word, or sequence that reads the same forward and backward. |
| 7 | + |
| 8 | +### Constraints |
| 9 | + |
| 10 | +- 1 <= `s.length` <= 1000 |
| 11 | +- `s` consists of lower English characters |
| 12 | + |
| 13 | +### Examples |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +### Solution |
| 19 | + |
| 20 | +If we look at the example above, we notice that any substring of length 3 contains a substring of length 1 at the center. |
| 21 | +Although we had already checked all substrings of length 1, our algorithm rechecked them for substrings of longer lengths. |
| 22 | +This rechecking consumes a lot of time, which can be avoided by storing and reusing the results of the earlier computations. |
| 23 | +To do this, we can create a lookup table, dp, of size n×n, where n is the length of the input string. Each cell dp[i][j] |
| 24 | +will store whether the string s[i..j] is a palindromic substring. If the cell dp[i][j] holds the result of the earlier |
| 25 | +computation, we will utilize it in O(1) lookup time instead of making a recursive call again. |
| 26 | + |
| 27 | +1. First, we initialize a count variable with 0, which will count the number of palindromic substrings in s. |
| 28 | +2. A lookup table is initialized with FALSE. |
| 29 | +3. Base case 1: The diagonal in the lookup table is populated with TRUE because any cell in the diagonal corresponds to |
| 30 | + a substring of length one, and any string of length one is always a palindrome. The number of one-letter palindromic |
| 31 | + strings (i.e., the number of elements in the diagonal) is also added to the count. |
| 32 | +4. Base case 2: We check whether all two-letter substrings are palindromes and update the count and dp accordingly. We |
| 33 | + do this by iterating over the string, comparing s[i] and s[i+1], and storing the result at dp[i][i+1]. After that, we |
| 34 | + also increment the count if the value of dp[i][i+1] is TRUE, which tells us that the two-letter substring was a |
| 35 | + palindrome. |
| 36 | +5. After these base cases, we check all substrings of lengths greater than two. However, we only compare the first and |
| 37 | + the last characters. The rest of the string is checked using the lookup table. For example, for a given string “zing”, |
| 38 | + we want to check whether “zin” is a palindrome. We’ll only compare ‘z’ and ‘n’ and check the value of dp[1][1], which |
| 39 | + will tell whether the remaining string “i”, represented by s[1..1], is a palindrome. We’ll take the logical AND of |
| 40 | + these two results and store it at dp[0][2] because “zin” is represented by the substring s[0..2]. This way, we’ll |
| 41 | + avoid redundant computations and check all possible substrings using the lookup table. |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +#### Solution Summary |
| 62 | + |
| 63 | +To recap, the solution to this problem can be divided into the following five main parts: |
| 64 | + |
| 65 | +1. We count all one-letter substrings since any one-letter string is always a palindrome. These results are also stored |
| 66 | + in a lookup table to be used later. |
| 67 | +2. Next, the algorithm checks all two-letter substrings and updates the count and the lookup table accordingly. |
| 68 | +3. After these base cases, the algorithm checks all possible substrings of lengths greater than two. However, it only |
| 69 | + compares the first and last characters, and the rest of the substring is checked using the lookup table. |
| 70 | +4. Whenever a palindromic substring is found, the count and the lookup table are updated accordingly. |
| 71 | +5. After checking all possible substrings, the algorithm terminates and returns the count of the palindromic substrings. |
| 72 | + |
| 73 | +#### Complexity Analysis |
| 74 | + |
| 75 | +##### Time Complexity |
| 76 | + |
| 77 | +The time complexity of this algorithm is O(n^2), where n is the length of the input string. This is the time required to |
| 78 | +populate the lookup table. |
| 79 | + |
| 80 | +##### Space Complexity |
| 81 | + |
| 82 | +The space complexity of this algorithm is O(n^2), where n is the length of the input string. This is the space occupied |
| 83 | +by the lookup table. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## Longest Palindromic Substring |
| 88 | + |
| 89 | +Problem Description |
| 90 | + |
| 91 | +Given a string A of size N, find and return the longest palindromic substring in A. |
| 92 | + |
| 93 | +Substring of string A is A[i...j] where 0 <= i <= j < len(A) |
| 94 | + |
| 95 | +Palindrome string: |
| 96 | + |
| 97 | +A string which reads the same backwards. More formally, A is palindrome if reverse(A) = A. |
| 98 | + |
| 99 | +Incase of conflict, return the substring which occurs first ( with the least starting index). |
| 100 | + |
| 101 | +Input Format |
| 102 | +First and only argument is a string A. |
| 103 | + |
| 104 | +Output Format |
| 105 | +Return a string denoting the longest palindromic substring of string A. |
| 106 | + |
| 107 | +Example Input |
| 108 | +A = "aaaabaaa" |
| 109 | + |
| 110 | +Example Output |
| 111 | +"aaabaaa" |
| 112 | + |
| 113 | +Example Explanation |
| 114 | +We can see that longest palindromic substring is of length 7 and the string is "aaabaaa". |
0 commit comments