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 ...
随机推荐
- pandas 常用语句
pandas的功能非常强大,支持类似与sql的数据增.删.查.改,并且带有丰富的数据处理函数: 支持时间序列分析功能:支持灵活处理缺失数据等. pandas的基本数据结构是Series和DataFra ...
- c++模板函数声明定义分离编译错误详解
今天看到accelerated c++上有个简单的vector容器的实现Vec,就再vs2008上编译了下: ///// Vec.h #ifndef GUARD_VEC_H #define GUARD ...
- POJ 2407:Relatives(欧拉函数模板)
Relatives AC代码 Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16186 Accept ...
- 2012年东京区域赛 UVAlive6182~6191
暑假训练场 A(UVAL6182). 凯神看了敲掉的题目,还没有看过 #include <iostream> #include <memory.h> using namespa ...
- js 时间操作积累
console.log( new Date() ); //Tue Mar 20 2018 22:47:01 GMT+0800 (中国标准时间) // var date = new Date( '201 ...
- (转) C++中成员初始化列表的使用
C++在类的构造函数中,可以两种方式初始化成员数据(data member). 1,在构造函数的实现中,初始类的成员数据.诸如: class point{private: int x,y;public ...
- treeql 基于rest 标准的接口开发协议
treeql 可以让我们按照数据库的关系模型,生成一个tree 模型的json 数据,基于rest 标准, 从设计上,来说还是很不错的,但是从长远发展来说graphql 应该会更好,也有相关的比较 参 ...
- torodb docker 运行试用
torodb 可以方便的让你迁移到pg,同时使用标准原生的sql 查询 使用官方的docker-compose 进行测试 环境准备 docker-compose 文件 wget https://raw ...
- php 备份和恢复数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Tomcat7 catalina.out 日志分割
Tomcat7 catalina.out 日志分割 安装过程如下: 1.下载(最新版本) cronolog-1.6.2.tar.gz 2.解压缩 # tar zxvf cronolog-1. ...