Find substring with K-1 distinct characters
参考 Find substring with K distinct characters
Find substring with K distinct characters(http://www.cnblogs.com/pegasus923/p/8444653.html)
Given a string and number K, find the substrings of size K with K-1 distinct characters. If no, output empty list. Remember to emit the duplicate substrings, i.e. if the substring repeated twice, only output once.
- 字符串中等题。Sliding window algorithm + Hash。此题跟上题的区别在于,子串中有一个重复字符。
- 思路还是跟上题一样,只是需要把对count的判断条件改成dupCount。当窗口size为K时,如果重复字符只有一个的话,则为结果集。对dupCount操作的判断条件,也需要改为>0, >1。
- //
- // main.cpp
- // LeetCode
- //
- // Created by Hao on 2017/3/16.
- // Copyright © 2017年 Hao. All rights reserved.
- //
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- using namespace std;
- class Solution {
- public:
- vector<string> subStringK1Dist(string S, int K) {
- vector<string> vResult;
- // corner case
- if (S.empty()) return vResult;
- unordered_map<char, int> hash;
- // window start/end pointer, hit count
- int left = , right = , dupCount = ;
- while (right < S.size()) {
- if (hash[S.at(right)] > ) // hit the condition dup char
- ++ dupCount;
- ++ hash[S.at(right)]; // count the occurrence
- ++ right; // move window end pointer rightward
- // window size reaches K
- if (right - left == K) {
- if ( == dupCount) { // find 1 dup char
- if (find(vResult.begin(), vResult.end(), S.substr(left, K)) == vResult.end()) // using STL find() to avoid dup
- vResult.push_back(S.substr(left, K));
- }
- // dupCount is only increased when hash[i] > 0, so only hash[i] > 1 means that dupCount was increased.
- if (hash[S.at(left)] > )
- -- dupCount;
- -- hash[S.at(left)]; // decrease to restore occurrence
- ++ left; // move window start pointer rightward
- }
- }
- return vResult;
- }
- };
- int main(int argc, char* argv[])
- {
- Solution testSolution;
- vector<string> sInputs = {"aaabbcccc","awaglknagawunagwkwagl", "abccdef", "", "aaaaaaa", "ababab"};
- vector<int> iInputs = {, , , , , };
- vector<string> result;
- /*
- {abbc }
- {awag naga agaw gwkw wkwa }
- {cc }
- {}
- {aa }
- {aba bab }
- */
- for (auto i = ; i < sInputs.size(); ++ i) {
- result = testSolution.subStringK1Dist(sInputs[i], iInputs[i]);
- cout << "{";
- for (auto it : result)
- cout << it << " ";
- cout << "}" << endl;
- }
- return ;
- }
Find substring with K-1 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: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- LeetCode "Longest Substring with At Most K Distinct Characters"
A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...
- [Swift]LeetCode340.最多有K个不同字符的最长子串 $ 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 ...
- Find substring with K distinct characters
Given a string and number K, find the substrings of size K with K distinct characters. If no, output ...
- [leetcode]340. 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 ...
- 最多有k个不同字符的最长子字符串 · Longest Substring with at Most k Distinct Characters(没提交)
[抄题]: 给定一个字符串,找到最多有k个不同字符的最长子字符串.eg:eceba, k = 3, return eceb [暴力解法]: 时间分析: 空间分析: [思维问题]: 怎么想到两根指针的: ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- [LeetCode] 340. 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] 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 ...
随机推荐
- python metaclass
看了很多类似的博客,这篇算是写的比较完善的,转载以备后期查看 原文: 一 你可以从这里获取什么? 1. 也许你在阅读别人的代码的时候碰到过metaclass,那你可以参考这里的介绍. 2. 或许你需要 ...
- DevExpress v18.1新版亮点——ASP.NET篇(四)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress ASP.NET v18.1 的新功能,快来下载试用新版本!点 ...
- Installing MIB in Ubuntu and Solving the Error “SNMP Cannot Find Module …”
Has noticed an error after executing the command snmpwalk with the indication of MIB instead of OID: ...
- iOS开发多线程篇—GCD简介
iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...
- NBUT 1217 Dinner 2010辽宁省赛
Time limit 1000 ms Memory limit 32768 kB Little A is one member of ACM team. He had just won the g ...
- vue2.*初体验
一. 推荐开发环境 二. 安装环境 安装 nvm :curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install. ...
- matplotlib.pyplot中add_subplot方法参数111的含义
下述代码若要运行,得在安装Python之外安装matplotlib.numpy.scipy.six等库,专门来看这篇小贴的朋友应该知道这些库. 参数331的意思是:将画布分割成3行3列,图像画在从左到 ...
- shell 脚本实战笔记(5)--搭建资源的镜像服务器
背景: 由于访问国外站点资源, 有时特别慢. 偶尔一次下载, 肯定还能忍受, 对于多次使用或者小团队内部使用, 搭建一个镜像站点, 无疑是个明智的决定. 这边以搭建CDH5的yum源镜像, 作为例子, ...
- C# 解析excel时,字段内有内容,却读取不到的解决方法
C# 解析excel时,字段内有内容,却读取不到的解决方法:"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ...
- 20155316 2016-2017-2 《Java程序设计》第5周学习总结
教材学习内容总结 这周总结 try catch语法 异常继承结构 throw finally AutoCloseable接口 Collection Map Lambda表达式 上周总结 三个关键 类与 ...