【Lintcode】1375. Substring With At Least K Distinct Characters
题目地址:
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相关推荐
- [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 ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- 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 ...
- 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= ...
- [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 ...
- 【CF913G】Power Substring 数论+原根
[CF913G]Power Substring 题意:T组询问,每次给定一个数a,让你求一个k,满足$2^k$的10进制的后$min(100,length(k))$位包含a作为它的子串.你只需要输出一 ...
- 【LeetCode】【HOT】215. 数组中的第K个最大元素(优先队列)
[LeetCode][HOT]215. 数组中的第K个最大元素 文章目录 [LeetCode][HOT]215. 数组中的第K个最大元素 package hot;import java.util.Co ...
- 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 ...
- [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 ...
最新文章
- c语言中循环结构的作用,C语言循环结构知识点
- C/C++ 宏定义中#、##、#@的区别
- kafka安装_安装Kafka
- Standard C++ Episode 7
- 爬虫神经网络_股市筛选和分析:在投资中使用网络爬虫,神经网络和回归分析...
- java sqlserver 遍历_mysql和sqlserver循环插入数据
- [Ext JS6]包-Package
- python-最速下降法
- iOS country code及国际区号
- 网易云音乐评论抓取(js逆向)
- LINUX修改主机名称(立即永久生效)
- robotframework(rf)中对时间操作的datetime库常用关键字
- test English
- 如何成为有效学习的高手 学习笔记
- Java实现阿里短息服务发送功能
- 第 218 场周赛阿里巴巴专场(只做出了前三道)设计 Goal 解析器+K 和数对的最大数目+连接连续二进制数字
- 你知道:学校门口开什么店赚钱吗?分享学校门口暴利生意!
- 基于集成运放心电放大器设计(原创)
- 《炬丰科技-半导体工艺》提高MLCC电介质的可靠性
- √开根号如何正常显示