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. DevExpress v18.1新版亮点——WPF篇(一)

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

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

  3. 20165210 Java第七周学习总结

    20165210 Java第七周学习总结 教材内容学习 第十一章学习总结 连接数据库: 1. 下载JDBC-MySQL数据库驱动 2. 加载JDBC-MySQL数据库驱动 3. 连接数据库 4. 注意 ...

  4. freeradius的https查询功能

    一.服务器要求 Radius服务器:centos6.6.ip.hostname.selinux  disabled.stop iptables freeradius版本:3.0.12 二.源码安装fr ...

  5. 深入理解uwsgi和gunicorn网络模型

    前言: 去年10月份建了一个python技术群,到现在为止人数已经涨到700人了.最一开始我经常在群里回应大家的问题,不管是简单还是困难的,我都会根据自己的经验来交流. 让人新奇的是一些初学者关注最多 ...

  6. ThinkPHP CodeIgniter URL访问举例

      ThinkPHP   URL访问:   http://localhost/think/index.php/Home/login/func/[name/syt/password/123/]   ht ...

  7. 安装wordcloud第三方库Unable to find vcvarsall.bat

    前言 本来想要使用python爬一些数据的,制作词云,感觉挺好玩的,不过python安装第三方库的时候遇到了一些问题,有的问题比较好解决,有的就找了好久才知道怎么解决的,故记录下来. 环境 系统:wi ...

  8. XINU安装程序.exe一键配置好XINU实验环境 - imsoft.cnblogs

      还在一步一步配置复杂的XINU吗?XINU安装程序.exe一键配置好XINU实验环境. XINU安装程序.exe使用说明: 本程序配合<网络协议分析与实现>/(胡维华等编著)课本使用 ...

  9. vulcanjs 开源工具方便快速开发react graphql meteor 应用

    vulcan 开源工具方便快速开发react graphql meteor 应用 操作环境mac os 安装 meteor 安装(此安装有点慢,可以通过正确上网解决) curl https://ins ...

  10. baidu手机浏览器安卓4.5版公布:由于快,所以爱

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDUyMzk4OA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...