C# || Palindromic Substrings – How To Find The Number Of Palindromic Substrings Using C#

Print Friendly, PDF & Email

The following is a module with functions which demonstrates how to find the number of palindromic substrings using C#.


1. Count Substrings – Problem Statement

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.

A substring is a contiguous sequence of characters within the string.

Example 1:


Input: s = "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".

Example 2:


Input: s = "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".


2. Count Substrings – Solution

The following is a solution which demonstrates how to find the number of palindromic substrings.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


3
6

Was this article helpful?
👍 YesNo

Leave a Reply