Skip to content

Commit 33163d4

Browse files
authored
Merge pull request #3942 from ImmidiSivani/leetcode-647
added solution to 647
2 parents 60a6c30 + c46e05e commit 33163d4

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
---
2+
id: palindromic-substrings
3+
title: Palindromic Substrings
4+
sidebar_label: 647-Palindromic Substrings
5+
tags:
6+
- String Manipulation
7+
- Dynamic Programming
8+
- LeetCode
9+
- Java
10+
- Python
11+
- C++
12+
description: "This is a solution to the Palindromic Substrings problem on LeetCode."
13+
sidebar_position: 11
14+
---
15+
16+
## Problem Description
17+
18+
Given a string `s`, return the number of palindromic substrings in it.A string is a palindrome when it reads the same backward as forward.
19+
20+
A substring is a contiguous sequence of characters within the string.
21+
22+
### Examples
23+
24+
**Example 1:**
25+
26+
```
27+
Input: s = "abc"
28+
Output: 3
29+
Explanation: Three palindromic strings: "a", "b", "c".
30+
```
31+
32+
**Example 2:**
33+
34+
```
35+
Input: s = "aaa"
36+
Output: 6
37+
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
38+
```
39+
40+
### Constraints
41+
42+
- `1 <= s.length <= 1000`
43+
- `s` consists of lowercase English letters.
44+
45+
---
46+
47+
## Solution for Count Palindromic Substrings Problem
48+
49+
To solve this problem, we need to count all the palindromic substrings in the given string `s`.
50+
51+
### Approach: Expand Around Center
52+
53+
The idea is to consider each possible center of a palindrome and expand outwards as long as we have a valid palindrome. For each center, count the palindromic substrings.
54+
55+
#### Steps:
56+
57+
1. **Identify Centers:**
58+
- There are 2*n - 1 centers for palindromes in a string of length `n` (including the space between characters for even-length palindromes).
59+
60+
2. **Expand Around Center:**
61+
- For each center, expand outwards to check if the substring is a palindrome. If it is, increment the count.
62+
63+
### Brute Force Approach
64+
65+
The brute force approach involves checking all substrings and verifying if each substring is a palindrome. This is inefficient and has a time complexity of $O(n^3)$.
66+
67+
### Optimized Approach
68+
69+
The optimized approach, using the Expand Around Center method, has a time complexity of $O(n^2)$.
70+
71+
### Code in Different Languages
72+
73+
<Tabs>
74+
<TabItem value="C++" label="C++" default>
75+
<SolutionAuthor name="@ImmidiSivani"/>
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
int countSubstrings(string s) {
81+
int n = s.size();
82+
int count = 0;
83+
84+
for (int i = 0; i < n; ++i) {
85+
count += countPalindromesAroundCenter(s, i, i); // Odd length
86+
count += countPalindromesAroundCenter(s, i, i + 1); // Even length
87+
}
88+
89+
return count;
90+
}
91+
92+
private:
93+
int countPalindromesAroundCenter(const string& s, int left, int right) {
94+
int count = 0;
95+
96+
while (left >= 0 && right < s.size() && s[left] == s[right]) {
97+
++count;
98+
--left;
99+
++right;
100+
}
101+
102+
return count;
103+
}
104+
};
105+
```
106+
107+
</TabItem>
108+
<TabItem value="Java" label="Java">
109+
<SolutionAuthor name="@ImmidiSivani"/>
110+
111+
```java
112+
class Solution {
113+
public int countSubstrings(String s) {
114+
int n = s.length();
115+
int count = 0;
116+
117+
for (int i = 0; i < n; i++) {
118+
count += countPalindromesAroundCenter(s, i, i); // Odd length
119+
count += countPalindromesAroundCenter(s, i, i + 1); // Even length
120+
}
121+
122+
return count;
123+
}
124+
125+
private int countPalindromesAroundCenter(String s, int left, int right) {
126+
int count = 0;
127+
128+
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
129+
count++;
130+
left--;
131+
right++;
132+
}
133+
134+
return count;
135+
}
136+
}
137+
```
138+
139+
</TabItem>
140+
<TabItem value="Python" label="Python">
141+
<SolutionAuthor name="@ImmidiSivani"/>
142+
143+
```python
144+
class Solution:
145+
def countSubstrings(self, s: str) -> int:
146+
n = len(s)
147+
count = 0
148+
149+
for i in range(n):
150+
count += self.countPalindromesAroundCenter(s, i, i) # Odd length
151+
count += self.countPalindromesAroundCenter(s, i, i + 1) # Even length
152+
153+
return count
154+
155+
def countPalindromesAroundCenter(self, s: str, left: int, right: int) -> int:
156+
count = 0
157+
158+
while left >= 0 and right < len(s) and s[left] == s[right]:
159+
count += 1
160+
left -= 1
161+
right += 1
162+
163+
return count
164+
```
165+
166+
</TabItem>
167+
</Tabs>
168+
169+
#### Complexity Analysis
170+
171+
- **Time Complexity**: $O(n^2)$, where `n` is the length of the input string `s`.
172+
- **Space Complexity**: $O(1)$, as we only use constant extra space.
173+
174+
---
175+
176+
<h2>Authors:</h2>
177+
178+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
179+
{['ImmidiSivani'].map(username => (
180+
<Author key={username} username={username} />
181+
))}
182+
</div>

0 commit comments

Comments
 (0)