参考 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。
  1. //
  2. // main.cpp
  3. // LeetCode
  4. //
  5. // Created by Hao on 2017/3/16.
  6. // Copyright © 2017年 Hao. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <vector>
  11. #include <unordered_map>
  12. using namespace std;
  13.  
  14. class Solution {
  15. public:
  16. vector<string> subStringK1Dist(string S, int K) {
  17. vector<string> vResult;
  18.  
  19. // corner case
  20. if (S.empty()) return vResult;
  21.  
  22. unordered_map<char, int> hash;
  23.  
  24. // window start/end pointer, hit count
  25. int left = , right = , dupCount = ;
  26.  
  27. while (right < S.size()) {
  28. if (hash[S.at(right)] > ) // hit the condition dup char
  29. ++ dupCount;
  30.  
  31. ++ hash[S.at(right)]; // count the occurrence
  32.  
  33. ++ right; // move window end pointer rightward
  34.  
  35. // window size reaches K
  36. if (right - left == K) {
  37. if ( == dupCount) { // find 1 dup char
  38. if (find(vResult.begin(), vResult.end(), S.substr(left, K)) == vResult.end()) // using STL find() to avoid dup
  39. vResult.push_back(S.substr(left, K));
  40. }
  41.  
  42. // dupCount is only increased when hash[i] > 0, so only hash[i] > 1 means that dupCount was increased.
  43. if (hash[S.at(left)] > )
  44. -- dupCount;
  45.  
  46. -- hash[S.at(left)]; // decrease to restore occurrence
  47.  
  48. ++ left; // move window start pointer rightward
  49. }
  50. }
  51.  
  52. return vResult;
  53. }
  54. };
  55.  
  56. int main(int argc, char* argv[])
  57. {
  58. Solution testSolution;
  59.  
  60. vector<string> sInputs = {"aaabbcccc","awaglknagawunagwkwagl", "abccdef", "", "aaaaaaa", "ababab"};
  61. vector<int> iInputs = {, , , , , };
  62. vector<string> result;
  63.  
  64. /*
  65. {abbc }
  66. {awag naga agaw gwkw wkwa }
  67. {cc }
  68. {}
  69. {aa }
  70. {aba bab }
  71. */
  72. for (auto i = ; i < sInputs.size(); ++ i) {
  73. result = testSolution.subStringK1Dist(sInputs[i], iInputs[i]);
  74.  
  75. cout << "{";
  76. for (auto it : result)
  77. cout << it << " ";
  78. cout << "}" << endl;
  79. }
  80.  
  81. return ;
  82. }

Find substring with K-1 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: 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 ...

  3. LeetCode "Longest Substring with At Most K Distinct Characters"

    A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...

  4. [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 ...

  5. 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 ...

  6. [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 ...

  7. 最多有k个不同字符的最长子字符串 · Longest Substring with at Most k Distinct Characters(没提交)

    [抄题]: 给定一个字符串,找到最多有k个不同字符的最长子字符串.eg:eceba, k = 3, return eceb [暴力解法]: 时间分析: 空间分析: [思维问题]: 怎么想到两根指针的: ...

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

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

  9. [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 ...

  10. [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. python metaclass

    看了很多类似的博客,这篇算是写的比较完善的,转载以备后期查看 原文: 一 你可以从这里获取什么? 1. 也许你在阅读别人的代码的时候碰到过metaclass,那你可以参考这里的介绍. 2. 或许你需要 ...

  2. DevExpress v18.1新版亮点——ASP.NET篇(四)

    用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress ASP.NET v18.1 的新功能,快来下载试用新版本!点 ...

  3. 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: ...

  4. iOS开发多线程篇—GCD简介

    iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...

  5. 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 ...

  6. vue2.*初体验

    一. 推荐开发环境 二. 安装环境 安装 nvm :curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install. ...

  7. matplotlib.pyplot中add_subplot方法参数111的含义

    下述代码若要运行,得在安装Python之外安装matplotlib.numpy.scipy.six等库,专门来看这篇小贴的朋友应该知道这些库. 参数331的意思是:将画布分割成3行3列,图像画在从左到 ...

  8. shell 脚本实战笔记(5)--搭建资源的镜像服务器

    背景: 由于访问国外站点资源, 有时特别慢. 偶尔一次下载, 肯定还能忍受, 对于多次使用或者小团队内部使用, 搭建一个镜像站点, 无疑是个明智的决定. 这边以搭建CDH5的yum源镜像, 作为例子, ...

  9. C# 解析excel时,字段内有内容,却读取不到的解决方法

    C# 解析excel时,字段内有内容,却读取不到的解决方法:"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ...

  10. 20155316 2016-2017-2 《Java程序设计》第5周学习总结

    教材学习内容总结 这周总结 try catch语法 异常继承结构 throw finally AutoCloseable接口 Collection Map Lambda表达式 上周总结 三个关键 类与 ...