Leetcode 3084. Count Substrings Starting and Ending with Given Character

  • [Leetcode 3084. Count Substrings Starting and Ending with Given Character](#Leetcode 3084. Count Substrings Starting and Ending with Given Character)
    • [1. 解题思路](#1. 解题思路)
    • [2. 代码实现](#2. 代码实现)

1. 解题思路

这一题其实挺简单的,只要看一下目标的character在string当中出现了几次即可,然后就是一个 C n 2 C_n^2 Cn2的头尾选择问题即可。

2. 代码实现

给出python代码实现如下:

python 复制代码
class Solution:
    def countSubstrings(self, s: str, c: str) -> int:
        cnt = Counter(s)[c]
        return cnt * (cnt+1) // 2

提交代码评测得到:耗时52ms,占用内存17.5MB。

相关推荐
Espresso Macchiato10 天前
Leetcode 3532. Path Existence Queries in a Graph I
leetcode medium·dsu·leetcode 3532·leetcode周赛447·uf
Espresso Macchiato17 天前
Leetcode 3523. Make Array Non-decreasing
leetcode··leetcode medium·leetcode 3523·leetcode周赛446
窗户2 个月前
有限Abel群的结构(1)
数学·抽象代数·排列组合
Tisfy4 个月前
LeetCode 2266.统计打字方案数:排列组合
数学·算法·leetcode·动态规划·题解·排列组合
窗户4 个月前
排列和组合的实现
排列组合·函数式·haskell·scheme
Espresso Macchiato4 个月前
Leetcode 3418. Maximum Amount of Money Robot Can Earn
leetcode·动态规划·leetcode medium·leetcode 3418·leetcode周赛432
Espresso Macchiato5 个月前
Leetcode 3388. Count Beautiful Splits in an Array
leetcode·z算法·leetcode medium·leetcode周赛428·leetcode 3388
Espresso Macchiato7 个月前
Leetcode 3316. Find Maximum Removals From Source String
leetcode·动态规划·leetcode medium·leetcode 3316·leetcode双周赛141
Espresso Macchiato7 个月前
Leetcode 3306. Count of Substrings Containing Every Vowel and K Consonants II
leetcode·滑动窗口·leetcode medium·leetcode 3306·leetcode周赛417
Espresso Macchiato7 个月前
Leetcode 3301. Maximize the Total Height of Unique Towers
leetcode·leetcode medium·leetcode双周赛140·leetcode 3301