Given a string and number K, find the substrings of size K with K 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。
  • 使用移动窗口算法,两个指针标记window起点left/终点right,还有一个计数器count记录hit count,初始值为K。另外还有一个hash数组来记录当前window中所有char。
  • 注意hit的条件是hash[i] == 0,代表当前window中没有重复char。如果hit,则-- count代表找到一个满足条件char。
  • ++ hash[right]来标记已经在当前window中出现过,并扩展right。
  • 如果window size为K,那么就可以判断如果count == 0,代表已经找到K个不重复的char,可以放入结果集。这里注意下需要用STL算法find()去重。
  • 接着要把window往右移,同时对right char做过的操作进行恢复。
  • 注意如果hash[left] == 1,代表以前满足过hash[right] == 0,所以需要-- count来恢复。而对于hash[left] > 1,因为重复char只会hit一次,只会对count + 1,所以不需要-- count,只要等到hash[left] == 1的时候再count - 1就行。同时因为left要移出window了,所以-- hash[left]来恢复,并右移left扩展到下一个window。
  • find - C++ Reference
    • http://www.cplusplus.com/reference/algorithm/find/?kw=find
 //
// 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> subStringKDist(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 = , count = K; while (right < S.size()) {
if (hash[S.at(right)] == ) // hit the condition 1 dup char
-- count; ++ hash[S.at(right)]; // increase hash value to mark that the char exists in the current window ++ right; // move window end pointer rightward // window size reaches K
if (right - left == K) {
if ( == count) { // find K distinct chars
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));
} // be careful for the restore condition. Count is only increased when hash[i] == 0, so only hash[i] == 1 means that count was increased.
if (hash[S.at(left)] == )
++ count; -- hash[S.at(left)]; // decrease to restore hash value ++ left; // move window start pointer rightward
}
} return vResult;
}
}; int main(int argc, char* argv[])
{
Solution testSolution; vector<string> sInputs = {"awaglknagawunagwkwagl", "abccdef", "", "aaaaaaa"};
vector<int> iInputs = {, , , };
vector<string> result; /*
{wagl aglk glkn lkna knag gawu awun wuna unag nagw agwk kwag }
{ab bc cd de ef }
{}
{}
*/
for (auto i = ; i < sInputs.size(); ++ i) {
result = testSolution.subStringKDist(sInputs[i], iInputs[i]); cout << "{";
for (auto it : result)
cout << it << " ";
cout << "}" << endl;
} return ;
}

Find substring with K distinct characters的更多相关文章

  1. Find substring with K-1 distinct characters

    参考 Find substring with K distinct characters Find substring with K distinct characters(http://www.cn ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Android Studio 常用快捷键大全

    鉴于使用AndroidStudio进行Android 开发的人越来越多,笔者整理了一些常用的快捷键.方便读者查阅,提高开发效率.

  2. (翻译)Angular 1.3中的验证器管道

    原文地址:VALIDATORS PIPELINE IN ANGULAR 1.3 我们知道在Angular中操作表单是很爽的.因为Angular本身的作用域模型,我们总能在相应的作用域中获取到表单当前的 ...

  3. L224 词汇题

    Elaborate 精心的 preparations were being made for the Prime Minister’s official visit to the four forei ...

  4. php property_exists

    property_exists("Device",$prop))判断Device 类中是否存在 $prop 这个属性该函数用来判断一个类中是否存在某个属性. 这里分析了php面向对 ...

  5. js中的转换方法

    1.var colors =['red','blue','green']; console.log(colors.toString()); // red,blue,green 总结:toString( ...

  6. C高级第一次PTA作业

    作业要求一 附加题目 写程序证明P++等价于(p)++还是等价于*(p++)? 1.设计思路: (1).题目算法描述 第一步:定义变量p并赋初值 第二步:分三次计算每次分别输出 p++,(p)++,* ...

  7. Linux Foundation(笔记)

    /************************************************************* * Linux Foundation * 1. 总结一下Linux的基础内 ...

  8. Android Studio安装、配置教程全 - 安卓开发环境的配置手册

    Android Studio的安装.配置 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 一 Downloa ...

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

    教材学习内容总结 程序设计本身的错误,建议使用Exception或其子类实例来表现. Java中所有错误都会被打包成对象. 如果父类异常对象在子类异常对象前被捕捉,则catch子类异常对象的区块将永远 ...

  10. Cannot find config.m4. Make sure that you run '/usr/local/php/bin/phpize' in the top level source directory of the module的 解决方法

    cp /php-7.1.22/ext/openssl/config0.m4 /usr/local/php/bin/config.m4