Talentd
FleetCode

3084. Count Substrings Starting and Ending with Given Character

Medium50.5% acceptance

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 <= 105
  • s and c consist only of lowercase English letters.

Hints open one at a time

Hint 1
Count the number of characters 'c' in string s, let’s call it m.
Hint 2
We can select 2 numbers i and j such that i <= j are the start and end indices of substring. Note that i and j can be the same.
Hint 3
The answer is m * (m + 1) / 2.
11:39
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def countSubstrings(self, s: str, c: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"abada"
"a"
Input
"zzz"
"z"
Your code is saved on this device.Solution