Given a string s, find out the count of all contiguous substrings whose starting and ending are same character.

Note: string contains lowercase English alphabets only.

Example

```
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
```

**Input:**

The first line of each test case contains an integer T denoting the number of test cases. Then T test cases follows. The first line of each test case contains a number N denoting the length of the string (S). Then the next line contains the string S.

**Output:**

For each test case output a new line containing a single integer, denoting the count of all the substrings whose first and last character are same.** **

**Constraints:**

1<=T<=100

1<=N<=10^{4}

**Example:
Input:**

2

5

abcab

13

geeksforgeeks

7

22

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Vagesh Verma | 98 |

UrGuru | 98 |

chaolinyu | 84 |

prernaus24 | 83 |

devendrachoudhary5485 | 74 |

abducodes | 624 |

harshtyagi36 | 298 |

highway_to_hell | 252 |

rajeswarii | 248 |

arpit_anshuman | 240 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.