# Recursive solution to count substrings with same first and last characters

We are given a string S, we need to find count of all contiguous substrings starting and ending with same character.

Examples :

```Input  : S = "abcab"
Output : 7
There are 15 substrings of "abcab"
a, ab, abc, abca, abcab, b, bc, bca
bcab, c, ca, cab, a, ab, b
Out of the above substrings, there
are 7 substrings : a, abca, b, bcab,
c, a and b.

Input  : S = "aba"
Output : 4
The substrings are a, b, a and aba
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We have discussed different solutions in below post.

Count substrings with same first and last characters

## C++

 `// c++ program to count substrings with same ` `// first and last characters ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `/* Function to count subtrings with same first and  ` `  ``last characters*/` `int` `countSubstrs(string str, ``int` `i, ``int` `j, ``int` `n) ` `{ ` `    ``// base cases ` `    ``if` `(n == 1) ` `        ``return` `1; ` `    ``if` `(n <= 0) ` `        ``return` `0; ` ` `  `    ``int` `res =  countSubstrs(str, i + 1, j, n - 1) +   ` `               ``countSubstrs(str, i, j - 1, n - 1) - ` `               ``countSubstrs(str, i + 1, j - 1, n - 2);             ` ` `  `    ``if` `(str[i] == str[j]) ` `        ``res++;  ` ` `  `    ``return` `res; ` `} ` ` `  `// driver code ` `int` `main() ` `{ ` `    ``string str = ``"abcab"``; ` `    ``int` `n = str.length(); ` `    ``cout << countSubstrs(str, 0, n - 1, n); ` `} `

## Java

 `// Java program to count substrings  ` `// with same first and last characters ` ` `  `class` `GFG ` `{ ` `    ``// Function to count subtrings ` `    ``// with same first and  ` `    ``// last characters ` `    ``static` `int` `countSubstrs(String str, ``int` `i,  ` `                                         ``int` `j, ``int` `n) ` `    ``{ ` `        ``// base cases ` `        ``if` `(n == ``1``) ` `            ``return` `1``; ` `        ``if` `(n <= ``0``) ` `            ``return` `0``; ` `     `  `        ``int` `res = countSubstrs(str, i + ``1``, j, n - ``1``) +  ` `                ``countSubstrs(str, i, j - ``1``, n - ``1``) - ` `                ``countSubstrs(str, i + ``1``, j - ``1``, n - ``2``);          ` `     `  `        ``if` `(str.charAt(i) == str.charAt(j)) ` `            ``res++;  ` `     `  `        ``return` `res; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `        ``String str = ``"abcab"``; ` `        ``int` `n = str.length(); ` `        ``System.out.print(countSubstrs(str, ``0``, n - ``1``, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python 3

 `# Python 3 program to count substrings with same ` `# first and last characters ` ` `  `# Function to count subtrings with same first and  ` `# last characters ` `def` `countSubstrs(``str``, i, j, n): ` ` `  `    ``# base cases ` `    ``if` `(n ``=``=` `1``): ` `        ``return` `1` `    ``if` `(n <``=` `0``): ` `        ``return` `0` ` `  `    ``res ``=` `(countSubstrs(``str``, i ``+` `1``, j, n ``-` `1``)  ` `        ``+` `countSubstrs(``str``, i, j ``-` `1``, n ``-` `1``)  ` `        ``-` `countSubstrs(``str``, i ``+` `1``, j ``-` `1``, n ``-` `2``))      ` ` `  `    ``if` `(``str``[i] ``=``=` `str``[j]): ` `        ``res ``+``=` `1` ` `  `    ``return` `res ` ` `  `# driver code ` `str` `=` `"abcab"` `n ``=` `len``(``str``) ` `print``(countSubstrs(``str``, ``0``, n ``-` `1``, n)) ` ` `  `# This code is contributed by Smitha `

## C#

 `// C# program to count substrings  ` `// with same first and last characters ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function to count subtrings ` `    ``// with same first and  ` `    ``// last characters ` `    ``static` `int` `countSubstrs(``string` `str, ``int` `i,  ` `                                 ``int` `j, ``int` `n) ` `    ``{ ` `         `  `        ``// base cases ` `        ``if` `(n == 1) ` `            ``return` `1; ` `        ``if` `(n <= 0) ` `            ``return` `0; ` `     `  `        ``int` `res = countSubstrs(str, i + 1, j, n - 1) ` `                ``+ countSubstrs(str, i, j - 1, n - 1) ` `            ``- countSubstrs(str, i + 1, j - 1, n - 2);      ` `     `  `        ``if` `(str[i] == str[j]) ` `            ``res++;  ` `     `  `        ``return` `res; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``string` `str = ``"abcab"``; ` `        ``int` `n = str.Length; ` `         `  `        ``Console.WriteLine( ` `                ``countSubstrs(str, 0, n - 1, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

```7
```

The time complexity of above solution is exponential. In Worst case, we may end up doing O(3n) operations. This article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.