< Ace Coding /> 🚀
ReplySubstrings that begin and end with the same letter
You are given a 0-indexed string s consisting of only lowercase English letters. Return the number of substrings in s that begin and end with the same character.
A substring is a contiguous non-empty sequence of characters within a string.
ExampLink
click to show
click to show
✅ Free leetcode questions which is 99% similar with the first question :
class Solution:
def countSubstrings(self, s: str, c: str) -> int:
count = s.count(c)
return count * (count + 1) // 2
https://leetcode.com/problems/count-substrings-starting-and-ending-with-given-character/description/
1 · 267 ·