当前位置: 首页 > news >正文

Leetcode 3306. Count of Substrings Containing Every Vowel and K Consonants II

  • Leetcode 3306. Count of Substrings Containing Every Vowel and K Consonants II
    • 1. 解题思路
    • 2. 代码实现
  • 题目链接:3306. Count of Substrings Containing Every Vowel and K Consonants II

1. 解题思路

这一题的话思路上就是一个滑动窗口,考察没一个点作为起始位置时,满足同时包含5个元音字符以及恰好 k k k个辅音字符的第一个位置,然后从该位置到其下一个辅音字符之间的任意一个位置都可以构成一个满足条件的substring。

因此,我们只需要控制这样一个滑动窗口并提前计算出一下每一个位置对应的下一个辅音字符出现的位置即可。

2. 代码实现

给出python代码实现如下:

class Solution:def countOfSubstrings(self, word: str, k: int) -> int:n = len(word)next_consonants = [n for _ in range(n)]idx = nfor i in range(n-1, -1, -1):next_consonants[i] = idxif word[i] not in "aeiou":idx = ii, j = 0, 0cnt = defaultdict(int)ans = 0while j < n:while j < n and (any(cnt[ch] <= 0 for ch in "aeiou") or cnt["c"] < k):if word[j] in "aeiou":cnt[word[j]] += 1else:cnt["c"] += 1j += 1while all(cnt[ch] > 0 for ch in "aeiou") and cnt["c"] >= k:if cnt["c"] == k and all(cnt[ch] > 0 for ch in "aeiou"):ans += (next_consonants[j-1] - (j-1))if word[i] in "aeiou":cnt[word[i]] -= 1else:cnt["c"] -= 1i += 1return ans

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

http://www.lryc.cn/news/452169.html

相关文章:

  • 算法笔记(五)——分治
  • 多级侧边菜单(递归)
  • JavaScript break与continue语句
  • 算法【从递归入手一维动态规划】
  • Linux中的进程间通信之共享内存
  • 第18周 3-过滤器
  • Linux之进程概念
  • 小程序-使用npm包
  • 【springboot】整合沙箱支付
  • 技术速递|Python in Visual Studio Code 2024年9月发布
  • 数据结构-3.5.队列的顺序实现
  • preconnect 预解析
  • Leecode热题100-283.移动零
  • 如何高效使用Prompt与AI大模型对话
  • Java 之深入理解 String、StringBuilder、StringBuffer
  • vue3项目执行pnpm update后还原package.json文件后运行报错
  • 蓝桥杯【物联网】零基础到国奖之路:十七. 扩展模块之单路ADC和NE555
  • SolveigMM Video Splitter方便快捷视频分割合并软件 V3.6.1309.3-供大家学习研究参考
  • Unity3D 创建一个人物,实现人物的移动
  • 【笔记】数据结构12
  • django的URL配置
  • 精华帖分享 | 因子构建思考1
  • kubernetes笔记(四)
  • 通信工程学习:什么是SNMP简单网络管理协议
  • ubuntu20.04系统下,c++图形库Matplot++配置
  • [激光原理与应用-126]:南京科耐激光-激光焊接 - 焊中无损检测技术 - 智能制程监测系统IPM介绍 - 26- 频域分析法
  • 深入理解 Solidity 修饰符(Modifier):功能、应用与最佳实践
  • YOLO11项目实战1:道路缺陷检测系统设计【Python源码+数据集+运行演示】
  • 怎么屏蔽统计系统统计到的虚假ip
  • 前端开发设计模式——策略模式