参考 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的更多相关文章

  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. auto类型说明符的注意事项

    1.auto类型说明符,是C++11标准下的,它能让编译器自行判断表达式的类型. 2.auto也能在一条语句上声明多个变量,但是,该语句上的多个变量的类型,必须一致. 3.编译器推断出来auto类型可 ...

  2. DevExpress v17.2新版亮点—WinForms篇(一)

    用户界面套包DevExpress v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.开篇介绍了DevExpress WinForms v17.2 Data Grid Control ...

  3. 获取bean的两种方式

    BeanFactory方式: 1: public void testFactory(){ ResourcePatternResolver rpt=new PathMatchingResourcePat ...

  4. Word文档加密小技巧

    文件菜单设置: 1.打开需要加密的Word文档. 2.选“文件”的“另存为”,出现“另存为”对话框,在“工具”中选“常规选项”,出现“保存”选项卡. 3.分别在“打开权限密码”和“修改权限密码”中输入 ...

  5. js 的垃圾回收器 原理 坑 优化-- 待续

    JavaScript垃圾回收的机制很简单: 找出不再使用的变量,然后释放掉其占用的内存,但是这个过程不是时时的, 因为其开销比较大,所以垃圾回收器会按照固定的时间间隔周期性的执行. 什么叫不再使用的变 ...

  6. Python bool值

    a = 10 print(type(a)) #<class 'int'> d = str(a) #把数字转换成str print(type(d)) #<class 'str'> ...

  7. [C#] 网站程序ASP.NET的性能诊断 - CPU分析

    微软提供了标准的CLR性能分析类库 https://github.com/Microsoft/clrmd 这个类库是开源的代码.能够获取CLR runtime里面几乎所有的信息. 如何获取clrmd编 ...

  8. matlab fopen()

    file=dir('/home/wang/Desktop/trainset/others/'); :length(file) path= strcat('/home/wang/Desktop/trai ...

  9. QT 5.4.1 for Android Ubuntu QtWebView Demo

    QT 5.4.1 for Android Ubuntu QtWebView Demo 2015-5-15 目录 一.说明: 二.参考文章: 三.QtWebView Demo在哪里? 四.Qt Crea ...

  10. 开源库dlib的安装与编译-CMake

    前言 最近项目涉及到关于face alignment的实现,了解到目前主要的算法有ERT.SDM.LBF等,其中由于dlib开源库实现了ERT算法,效果也很不错,故开始研究dlib的使用.而使用的第一 ...