C Program
#include <stdio.h> #include <string.h> #include <stdbool.h> bool isPalindrome(char str[], int start, int end) { while (start < end) { if (str[start++] != str[end--]) return false; } return true; } int main() { char str[100]; scanf("%s", str); int n = strlen(str), count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (isPalindrome(str, i, j)) count++; } } printf("%d\n", count); return 0; }
C Output
Input:
aaa
Output:
6
C++ Program
#include <iostream> using namespace std; bool isPalindrome(string s, int l, int r) { while (l < r) { if (s[l++] != s[r--]) return false; } return true; } int main() { string s; cin >> s; int n = s.size(), count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (isPalindrome(s, i, j)) count++; } } cout << count << endl; return 0; }
C++ Output
Input:
ababa
Output:
9
JAVA Program
import java.util.Scanner; public class Main { static boolean isPalindrome(String s, int l, int r) { while (l < r) { if (s.charAt(l++) != s.charAt(r--)) return false; } return true; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); int n = s.length(), count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (isPalindrome(s, i, j)) count++; } } System.out.println(count); sc.close(); } }
JAVA Output
Input:
aba
Output:
4
Python Program
def is_palindrome(s): return s == s[::-1] s = input().strip() count = 0 for i in range(len(s)): for j in range(i, len(s)): if is_palindrome(s[i:j+1]): count += 1 print(count)
Python Output
Input:
racecar
Output:
10
In-Depth Explanation
Example
Let’s take the string "aaa".
Substrings are: "a" (0,0), "a" (1,1), "a" (2,2), "aa" (0,1), "aa" (1,2), "aaa" (0,2).
Each of these is a palindrome, so the count = 6.
Now take "aba".
Palindromes: "a", "b", "a", "aba".
Count = 4.
Real-Life Analogy
Consider palindromes as mirror words. If, when you see the string in a mirror, it looks the same, then it's a palindrome. If you consider a sentence to be a bead necklace, palindromic substrings are like portions that appear symmetric when you turn them over. For example, "aba" is like a symmetric piece of a necklace, but "abc" is not.
Why It Matters
Palindromic substring counting is a learning experience for nested loops, extracting substrings, and repeatedly checking conditions. Palindromic substring detection is related to dynamic programming and Manacher's algorithm, which are high-level concepts applied in competitive programming. Palindromic substring detection in real life has uses in DNA sequence analysis, pattern detection, and data compression since symmetry tends to be a sign of something exceptional.
Learning Insights
This exercise illustrates the contrast between brute-force and optimized solutions. Brute force tries all substrings, resulting in O(n³) complexity (since we build substrings and verify them). Optimizations such as expanding around the center bring it down to O(n²), and sophisticated algorithms such as Manacher's bring it down to O(n). As a novice, learning brute force first gives you insight into the idea before proceeding to efficient solutions.
Interview Relevance
This is a popular interview question since it checks string manipulation, palindrome thinking, nested loops, and occasionally dynamic programming if the interviewer probes deeper. A programmer who can articulate brute force as well as optimized methods shows both problem-solving transparency and algorithmic understanding.
Real-World Applications
Natural Language Processing: Identifying palindromic sentences in texts for pattern matching.
Bioinformatics: Palindromes in DNA tend to indicate significant functional regions.
Security: Symmetry checks are occasionally used in hashing or data verification.
SEO-Optimized Conclusion
Palindromic substring count is a significant string problem that assists beginners in sharpening their logic for pattern recognition and nested iteration. Practicing this problem empowers students to confidently work with substrings, loops, and condition verification while understanding how to optimize brute force solutions. Whether interviewing for coding positions, competitive programming, or actual uses such as DNA sequence alignment and text processing, the knowledge of palindromic substrings establishes a solid algorithmic thinking and string processing foundation in C, C++, Java, and Python.
Social Plugin