Find substring with K-1 distinct characters
参考 Find substring with K distinct characters
Find substring with K distinct characters(http://www.cnblogs.com/pegasus923/p/8444653.html)
Given a string and number K, find the substrings of size K with K-1 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。此题跟上题的区别在于,子串中有一个重复字符。
- 思路还是跟上题一样,只是需要把对count的判断条件改成dupCount。当窗口size为K时,如果重复字符只有一个的话,则为结果集。对dupCount操作的判断条件,也需要改为>0, >1。
//
// 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> subStringK1Dist(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 = , dupCount = ; while (right < S.size()) {
if (hash[S.at(right)] > ) // hit the condition dup char
++ dupCount; ++ hash[S.at(right)]; // count the occurrence ++ right; // move window end pointer rightward // window size reaches K
if (right - left == K) {
if ( == dupCount) { // find 1 dup char
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));
} // dupCount is only increased when hash[i] > 0, so only hash[i] > 1 means that dupCount was increased.
if (hash[S.at(left)] > )
-- dupCount; -- hash[S.at(left)]; // decrease to restore occurrence ++ left; // move window start pointer rightward
}
} return vResult;
}
}; int main(int argc, char* argv[])
{
Solution testSolution; vector<string> sInputs = {"aaabbcccc","awaglknagawunagwkwagl", "abccdef", "", "aaaaaaa", "ababab"};
vector<int> iInputs = {, , , , , };
vector<string> result; /*
{abbc }
{awag naga agaw gwkw wkwa }
{cc }
{}
{aa }
{aba bab }
*/
for (auto i = ; i < sInputs.size(); ++ i) {
result = testSolution.subStringK1Dist(sInputs[i], iInputs[i]); cout << "{";
for (auto it : result)
cout << it << " ";
cout << "}" << endl;
} return ;
}
Find substring with K-1 distinct characters的更多相关文章
- [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 ...
- LeetCode "Longest Substring with At Most K Distinct Characters"
A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...
- [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 ...
- 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 ...
- [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 Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
随机推荐
- 团队作业之现场UML设计
项目UML设计(团队) 团队信息 团队名:第三视角 各成员学号及姓名 姓名 学号 博客链接 张扬(组长) 031602345 http://www.cnblogs.com/sxZhangYang/p/ ...
- c语言基础:数据类型 分类: iOS学习 c语言基础 2015-06-10 21:43 9人阅读 评论(0) 收藏
C语言基本数据类型大体上分为: 整型 和 浮点型 字节: 计算机中最小的储存单位 1 Byte = 8 bit 整型: int 4 ...
- .net core grpc 实现通信(一)
现在系统都服务化,.net core 实现服务化的方式有很多,我们通过grpc实现客户端.服务端通信. grpc(https://grpc.io/)是google发布的一个开源.高性能.通用RPC(R ...
- hdu 5184 类卡特兰数+逆元
BC # 32 1003 题意:定义了括号的合法排列方式,给出一个排列的前一段,问能组成多少种合法的排列. 这道题和鹏神研究卡特兰数的推导和在这题中的结论式的推导: 首先就是如何理解从题意演变到卡特兰 ...
- Windows下同一台机器上elasticsearch集群的配置以及elasticsearch-head插件的使用
ElasticSearch是一个基于Lucene的开源搜索服务器,现已经被越来越多的企业运用于项目当中,笔者为了学习es在自己机器上简单的搭建了一个es集群,此文权当记录. 1.我用到的压缩包 下载地 ...
- java泛型(一)、泛型的基本介绍和使用
现在开始深入学习java的泛型了,以前一直只是在集合中简单的使用泛型,根本就不明白泛型的原理和作用.泛型在java中,是一个十分重要的特性,所以要好好的研究下. 泛 型的定义:泛型是JDK 1.5的一 ...
- python 二维list取列
b = [i[0] for i in a] # 从a中的每一行取第一个元素.
- WIFI_认证加密学习_STA_AP_WDS
2-1.1_15_使用卡1_准备工作及配置内核====================================1.无线网卡连接上路由或AP之后使用上是和有线网卡是一样的,都是socket编程. ...
- 一个5.0/3.3V双向通讯的电路
来自群友 西江月-梧州 的分享 硬件程工-深圳福永(79993868) 17:06:33 当3.3V高时二极管阳极为3.3V,阴极接了10K上拉为5V,二极管的压降为反向,此时二极管不导通. 硬件程工 ...
- 【转】每天一个linux命令(12):more命令
原文网址:http://www.cnblogs.com/peida/archive/2012/11/02/2750588.html more命令,功能类似 cat ,cat命令是整个文件的内容从上到下 ...