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 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的更多相关文章
- Find substring with K-1 distinct characters
参考 Find substring with K distinct characters Find substring with K distinct characters(http://www.cn ...
- [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 ...
- [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 ...
- [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 K Distinct Characters"
A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...
随机推荐
- 循环队列 c 实现!!!!
上数据结构课的时候老师让写了一个循环队列子系统. 代码如下: #include<stdio.h> #include<malloc.h> #define MAXLEN 100 # ...
- Linux便捷命令
快捷键: tab:命令和文件名称补齐功能快捷键 Ctrl + c:中断当前程序 Ctrl + d:退出当前终端 求助快捷键: man:manual的简写,如man ls man命令: / string ...
- SHA1
整理于互动百科 安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准(Digital Signature Standard DSS)里面定义的数字签名算法(Digital ...
- stream_get_contents 和file_get_content的区别
stream_get_contents — 读取资源流到一个字符串, 即其读取的内容是一个已经打开的资源句柄,如fopen函数打开的文件句柄,. 而 file_get_content可以直接读取文件内 ...
- OC基础:实例变量和成员变量的区别 分类: ios学习 OC 2015-06-14 17:59 16人阅读 评论(0) 收藏
摘要: Objective-C 引入了"实例变量"的概念,但同时, 也经常出现 "成员变量"的声音. 到底什么是实例变量,什么是成员变量,二者的区别是什么呢 ...
- 第十七篇 make的路径搜索综合实践
本节我们编写路径搜索相关的makefile,具体需求如下: 1.工程项目中不希望源码文件夹在编译时被改动(只读文件夹). 2.在编译时自动创建文件夹(build)用于存放编译结果. 3.编译过 ...
- css怎么让li水平排列和div居中
让li向左浮动即可 给div定一个宽度,然后margin:0 auto;即可:
- Visual Studio Code 使用指南
安装 VSCode是微软推出的一款轻量编辑器,采取了和VS相同的UI界面,搭配合适的插件可以优化前端开发的体验. HTML Snippets:增强了zen-coding,增加了H5的自动补全,安 ...
- rabbitmq学习(二):rabbitmq(消息队列)的作用以及rabbitmq之直连交换机
前言 上篇介绍了AMQP的基本概念,组成及其与rabbitmq的关系.了解了这些东西后,下面我们开始学习rabbitmq(消息队列)的作用以及用java代码和rabbitmq通讯进行消息发布和接收.因 ...
- WPF优化体验<一>(转)
最近将一个开发和维护了五年的一个Winform项目进行重构,考虑到最近很流行将用户体验挂在嘴上,于是采用了WPF技术,希望能在外观和体验上有一个全新的效果. 以前使用Winform的时候内存控制得不错 ...