|
| 1 | +**Description:** |
| 2 | +Given a string `str`, return the number of substrings within`str` that are palindromes. |
| 3 | + |
| 4 | +**Note:**A palindrome is a string that reads the same backward as forward. |
| 5 | + |
| 6 | +### Examples |
| 7 | +Example 1: |
| 8 | +Input: str = "abcd" |
| 9 | +Output: 4 |
| 10 | + |
| 11 | +Example 2: |
| 12 | +Input: str = "aaa" |
| 13 | +Output: 3 |
| 14 | + |
| 15 | + |
1 | 16 | **Algorithmic Steps**
|
2 |
| -This problem is solved with the help of Two pointer approach(Expand around center). The algorithmic approach can be summarized as follows: |
| 17 | +This problem is solved with the help of **Expand around center** approach. The algorithmic approach can be summarized as follows: |
3 | 18 |
|
4 |
| -1. Write a preliminary check by returning the input string length when its size is less than 2. |
| 19 | +1. Write a preliminary check by just returning the input string length when its size is less than 2. |
5 | 20 |
|
6 |
| -2. Initialize count variable to 0, to store the palindromic substrings count. |
| 21 | +2. Initialize count(`count`) variable to 0, which is used to store the palindromic substrings count. |
7 | 22 |
|
8 |
| -3. Iterate over the input string using an iteration variable i(from 0 to length of string). |
| 23 | +3. Iterate over the input string using an iteration variable `i`(from 0 to end of string). |
9 | 24 |
|
10 |
| -4. For each iteration, calculate the total count of odd palindromic substrings using a separate common function(step6). This common function accepts current iteration variable `i` as left and right pointer values. |
| 25 | +4. For each iteration, calculate the total count of odd palindromic substrings using a separate common function(step6). This common function accepts current iteration variable `i` for both left and right pointer values. |
11 | 26 |
|
12 | 27 | 5. For each iteration, calculate the total count of even palindromic substrings using a separate common function(step6). This common function accepts current iteration variable `i` as left and `i+1` as right pointer values.
|
13 | 28 |
|
14 |
| -6. The common function iterate over the given string using starting(i.e, left) and ending(i.e, right) index arguments. The left pointer is going to be decremented and right pointer is going to be incremented whenever the respective character values are equal. |
| 29 | +6. The common function is created to iterate over the given string using starting(i.e, `left`) and ending(i.e, `right`) index arguments. The left pointer is going to be decremented and right pointer is going to be incremented whenever the respective character values are equal. |
15 | 30 |
|
16 |
| -7. The sum of even and odd palindromic substring count is going to be returned as total count for palindromic substrings. |
| 31 | +7. The sum of even and odd palindromic substring's count is going to be returned as total count for palindromic substrings. |
17 | 32 |
|
18 | 33 |
|
19 | 34 | **Time and Space complexity:**
|
20 |
| -This algorithm has a time complexity of O(n * 2)( O(n * 2) + O(n * 2)) because either odd or event need to iterate over the input string atleast once which takes O(n) and substrings calculate on each position takes O(n). Also, it takes space complexity of O(1) without using any data structure. |
| 35 | +This algorithm has a time complexity of `O(n * 2)`(i.e, `O(n * 2) + O(n * 2)`) because finding both odd and event length palindrome takes `O(n) +O(n)` time complexity and it needs to be repeated on each character position which takes O(n). |
| 36 | + |
| 37 | +Also, it takes space complexity of `O(1)` without using any additional data structure. |
0 commit comments