🧠
Expand Around Center (Optimal, Space Efficient)
💡 This approach uses the insight that palindromes expand from centers, avoiding DP tables and reducing space to O(1). It is optimal and often preferred in interviews.
Intuition
Every palindrome can be expanded from its center (single character or between two characters). Count palindromes by expanding around each center.
Algorithm
- Iterate over each index as a center for odd-length palindromes.
- Expand around center while characters match, count palindromes.
- Repeat for even-length palindromes (center between two characters).
- Sum all counts and return.
💡 This method avoids extra space and uses two-pointer expansion to count palindromes efficiently.
def countSubstrings(s: str) -> int:
n = len(s)
count = 0
def expand(left, right):
nonlocal count
while left >= 0 and right < n and s[left] == s[right]:
count += 1
left -= 1
right += 1
for i in range(n):
expand(i, i) # Odd length palindromes
expand(i, i + 1) # Even length palindromes
return count
# Driver code
if __name__ == '__main__':
print(countSubstrings('aaa')) # Expected output: 6
Line Notes
def expand(left, right):Helper to expand around center and count palindromes
while left >= 0 and right < n and s[left] == s[right]:Expand while substring is palindrome
count += 1Increment count for each palindrome found
for i in range(n):Check all possible centers
public class Solution {
public int countSubstrings(String s) {
int n = s.length();
int count = 0;
for (int i = 0; i < n; i++) {
count += expandAroundCenter(s, i, i); // Odd length
count += expandAroundCenter(s, i, i + 1); // Even length
}
return count;
}
private int expandAroundCenter(String s, int left, int right) {
int count = 0;
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
count++;
left--;
right++;
}
return count;
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.countSubstrings("aaa")); // Expected output: 6
}
}
Line Notes
for (int i = 0; i < n; i++) {Iterate over all centers
count += expandAroundCenter(s, i, i);Count odd length palindromes
count += expandAroundCenter(s, i, i + 1);Count even length palindromes
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {Expand while palindrome condition holds
#include <iostream>
#include <string>
using namespace std;
int expandAroundCenter(const string& s, int left, int right) {
int count = 0;
while (left >= 0 && right < (int)s.size() && s[left] == s[right]) {
count++;
left--;
right++;
}
return count;
}
int countSubstrings(string s) {
int n = s.size();
int count = 0;
for (int i = 0; i < n; i++) {
count += expandAroundCenter(s, i, i); // Odd length
count += expandAroundCenter(s, i, i + 1); // Even length
}
return count;
}
int main() {
cout << countSubstrings("aaa") << endl; // Expected output: 6
return 0;
}
Line Notes
for (int i = 0; i < n; i++) {Iterate over all centers
count += expandAroundCenter(s, i, i);Count odd length palindromes
count += expandAroundCenter(s, i, i + 1);Count even length palindromes
while (left >= 0 && right < (int)s.size() && s[left] == s[right]) {Expand while palindrome condition holds
function countSubstrings(s) {
const n = s.length;
let count = 0;
function expand(left, right) {
while (left >= 0 && right < n && s[left] === s[right]) {
count++;
left--;
right++;
}
}
for (let i = 0; i < n; i++) {
expand(i, i); // Odd length
expand(i, i + 1); // Even length
}
return count;
}
// Test
console.log(countSubstrings('aaa')); // Expected output: 6
Line Notes
function expand(left, right) {Expand around center to count palindromes
while (left >= 0 && right < n && s[left] === s[right]) {Expand while substring is palindrome
count++;Increment count for each palindrome found
for (let i = 0; i < n; i++) {Check all possible centers
Each center expansion takes O(n) in worst case, and there are O(n) centers.
💡 For n=20, about 400 expansions are done, very efficient and uses minimal memory.
Interview Verdict: Accepted
This is the most optimal and elegant solution, preferred in interviews for its simplicity and efficiency.