Description
You are given a string s and a character c. Return the total number of substrings of s that start and end with c.
Example 1:
Input: s = "abada", c = "a"
Output: 6
Explanation: Substrings starting and ending with "a" are: "abada", "abada", "abada", "abada", "abada", "abada".
Example 2:
Input: s = "zzz", c = "z"
Output: 6
Explanation: There are a total of 6 substrings in s and all start and end with "z".
Constraints:
1 <= s.length <= 105sandcconsist only of lowercase English letters.
Solution
Python3
class Solution:
def countSubstrings(self, s: str, c: str) -> int:
N = len(s)
count = res = 0
for x in s:
if x == c:
count += 1
res += count
return res