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 ...
随机推荐
- js创建表单并提交
1.脚本 Util = { post : function(URL, PARAMS){ //虚拟表单实现post提交 var temp = document.createElement("f ...
- Ansible之roles介绍
本节内容: 什么场景下会用roles? roles示例 一.什么场景下会用roles? 假如我们现在有3个被管理主机,第一个要配置成httpd,第二个要配置成php服务器,第三个要配置成MySQL服务 ...
- android编译环境安装
Android 编译环境安装 安装 Java 6 安装 Java 6 安装依赖包 (Ubuntu 12.04) $ sudo apt-get install git gnupg flex bison ...
- 人工智能是如何改变IT运维和DevOps的?
要解决IT运维和DevOps团队面对的挑战,就是能够在大量的日志数据流中识别潜在的小的有害问题,这正是认知洞察力所做的事情. 接下来的几年里,DevOps(开发软件工程.技术运营和质量保障三者的交集) ...
- 【linux基础】重命名文件和文件夹
linux下重命名文件或文件夹的命令mv既可以重命名,又可以移动文件或文件夹. 例子:将目录A重命名为B mv A B 例子:将/a目录移动到/b下,并重命名为c mv /a /b/c 其实在文本模式 ...
- [LeetCode&Python] Problem 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- HDU 4135:Co-prime(容斥+二进制拆分)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- MVC中未能加载程序集System.Web.Http/System.Web.Http.WebHost
==================================== 需要检查项目的Microsoft.AspNet.WebApi版本是否最新,System.Web.Http 这个命名空间需要更新 ...
- MyBatis 与 Spring Data JPA 选择谁?
MyBatis 与 Spring Data JPA 选择谁? https://www.v2ex.com/t/285081 jpa predicate优缺点 https://blog.csdn.net/ ...
- java类库 java API jar包 C语言函数库
翁凯说:java的强大是因为类库的强大 C/C++强大是因为函数库 在程序中用到系统提供的标准函数库中的输入输出函数时 应在程序的开头写上一行:#include"stdio.h"或 ...