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 Macchiato4 天前
Leetcode 3557. Find Maximum Number of Non Intersecting Substrings
动态规划·leetcode medium·leetcode 3557·leetcode双周赛157
Espresso Macchiato13 天前
Leetcode 3552. Grid Teleportation Traversal
广度优先遍历·leetcode medium·leetcode周赛450·leetcode 3552·堆排
Espresso Macchiato13 天前
Leetcode 3551. Minimum Swaps to Sort by Digit Sum
leetcode·排序·leetcode medium·leetcode 3551·leetcode周赛450
Espresso Macchiato16 天前
Leetcode 3543. Maximum Weighted K-Edge Path
leetcode·leetcode medium·图遍历·leetcode 3543·leetcode双周赛156
Espresso Macchiato1 个月前
Leetcode 3532. Path Existence Queries in a Graph I
leetcode medium·dsu·leetcode 3532·leetcode周赛447·uf
Espresso Macchiato1 个月前
Leetcode 3523. Make Array Non-decreasing
leetcode··leetcode medium·leetcode 3523·leetcode周赛446
窗户2 个月前
有限Abel群的结构(1)
数学·抽象代数·排列组合
Tisfy4 个月前
LeetCode 2266.统计打字方案数:排列组合
数学·算法·leetcode·动态规划·题解·排列组合
窗户5 个月前
排列和组合的实现
排列组合·函数式·haskell·scheme
Espresso Macchiato5 个月前
Leetcode 3418. Maximum Amount of Money Robot Can Earn
leetcode·动态规划·leetcode medium·leetcode 3418·leetcode周赛432