题目地址:

https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description

给定一个字符串sss,求其包含至少kkk个不同字符的子串的个数。

思路是双指针。开两个指针iii和jjj,其中iii是右指针,jjj是左指针。我们累加以jjj为左端点的满足条件的子串个数。当s[j:i]s[j:i]s[j:i]这个区间里包含至少kkk个不同字符时,此时∀k≥i,s[j:k]\forall k \ge i, s[j:k]∀k≥i,s[j:k]都成立,所以累加ls−il_s-ils​−i,此时左端点为jjj的情况已经枚举完毕,则右移jjj,继续做累加,直到区间不满足条件为止,则右移iii。代码如下:

import java.util.HashMap;
import java.util.Map;public class Solution {/*** @param s: a string* @param k: an integer* @return: the number of substrings there are that contain at least k distinct characters*/public long kDistinctCharacters(String s, int k) {// Write your code hereMap<Character, Integer> map = new HashMap<>();long res = 0;for (int i = 0, j = 0; i < s.length(); i++) {map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);while (map.size() >= k) {res += s.length() - i;char ch = s.charAt(j);map.put(ch, map.get(ch) - 1);if (map.get(ch) == 0) {map.remove(ch);}j++;}}return res;}
}

时空复杂度O(ls)O(l_s)O(ls​)。

【Lintcode】1375. Substring With At Least K Distinct Characters相关推荐

  1. [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串...

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  2. LeetCode 340. Longest Substring with At Most K Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...

  3. Longest Substring With At Most K Distinct Characters

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  4. 386 · Longest Substring with At Most K Distinct Characters最多有k个不同字符的最长子字符串

    链接:https://www.lintcode.com/problem/386/description https://mp.weixin.qq.com/s?__biz=MzU2OTUyNzk1NQ= ...

  5. [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串...

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  6. 【CF913G】Power Substring 数论+原根

    [CF913G]Power Substring 题意:T组询问,每次给定一个数a,让你求一个k,满足$2^k$的10进制的后$min(100,length(k))$位包含a作为它的子串.你只需要输出一 ...

  7. 【LeetCode】【HOT】215. 数组中的第K个最大元素(优先队列)

    [LeetCode][HOT]215. 数组中的第K个最大元素 文章目录 [LeetCode][HOT]215. 数组中的第K个最大元素 package hot;import java.util.Co ...

  8. LeetCode 159. Longest Substring with At Most Two Distinct Characters --Java,C++,Python解法

    题目地址:Longest Substring with At Most Two Distinct Characters - LeetCode Given a string s , find the l ...

  9. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串...

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

最新文章

  1. c语言中循环结构的作用,C语言循环结构知识点
  2. C/C++ 宏定义中#、##、#@的区别
  3. kafka安装_安装Kafka
  4. Standard C++ Episode 7
  5. 爬虫神经网络_股市筛选和分析:在投资中使用网络爬虫,神经网络和回归分析...
  6. java sqlserver 遍历_mysql和sqlserver循环插入数据
  7. [Ext JS6]包-Package
  8. python-最速下降法
  9. iOS country code及国际区号
  10. 网易云音乐评论抓取(js逆向)
  11. LINUX修改主机名称(立即永久生效)
  12. robotframework(rf)中对时间操作的datetime库常用关键字
  13. test English
  14. 如何成为有效学习的高手 学习笔记
  15. Java实现阿里短息服务发送功能
  16. 第 218 场周赛阿里巴巴专场(只做出了前三道)设计 Goal 解析器+K 和数对的最大数目+连接连续二进制数字
  17. 你知道:学校门口开什么店赚钱吗?分享学校门口暴利生意!
  18. 基于集成运放心电放大器设计(原创)
  19. 《炬丰科技-半导体工艺》提高MLCC电介质的可靠性
  20. √开根号如何正常显示

热门文章

  1. c语言中数组arr的表示含义
  2. pytorch入门强化教程——数据加载和处理
  3. 共享文件与打印机设置
  4. LARS Lasso
  5. 垃圾回收器判断对象是否存活
  6. nmealib linux编译,GPS nmealib学习笔记
  7. W25qxxx 实现文件系统 ---- 基于RT-thread嵌入式操作系统
  8. 基于51单片机的出租车计价器的设计
  9. 要不要启用苹果wapi_苹果“史上最强”系统ios13来了,要不要升级?
  10. ArcGIS模拟风场(流场)