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 ...
随机推荐
- 浅谈:从为什么学习python到如何学好python
虽然目前的编程语言有很多,但是基础语法上的概念,本质上都是相通的.可以做到一通百通.所以没有必要为了学哪门语言纠结太多. python是目前市面上,我个人认为是最简洁&&最优雅& ...
- 使用Setup安装Windows8 RTM方法
1.下载Windows 8 RTM版. 2.解压ISO映像或加载虚拟光驱. 3.为了安装双系统,打开sources文件夹中的setup.exe才可. 4.打开后选择“在线获取安装程序的最新更新(推荐) ...
- Java Socket 实现HTTP服务器核心
原文链接:http://www.ihuxu.com/p/235.html 首先了解下HTTP协议: wikiPedia的说明很好,在此不重复了.链接:http://zh.wikipedia.org ...
- WinRAR 0day漏洞 附利用过程
英国安全机构Mohammad Reza Espargham的漏洞实验室发现,流行压缩工具WinRAR 5.21最新版里存在一个安全漏洞,目前该漏洞还属于零日漏洞,同时官方正在全力修补该漏洞.同时报告该 ...
- Python_常用的正则表达式处理函数
正则表达式就是用查找字符串的,它能查找规则比较复杂的字符串反斜杠:正则表达式里面用"\"作为转义字符. s='<a class="h3" href=&qu ...
- UI基础:UI程序执行顺序(UIApplicationMain()函数),自定义视图 分类: iOS学习-UI 2015-07-02 22:09 68人阅读 评论(0) 收藏
UI程序的一般执行顺序: 先进入main里面,执行函数UIApplicationMain(),通过该函数创建应用程序对象和指定其代理并实现监听,当执行函数UIApplicationMain()时还会做 ...
- Java中线程同步的方法
同步方法 即有synchronized关键字修饰的方法. 由于java的每个对象都有一个内置锁,当用此关键字修饰方法时, 内置锁会保护整个方法.在调用该方法前,需要获得内置锁,否则就处于阻塞状态. 注 ...
- 按照Right-BICEP要求设计四则运算2程序的单元测试用例
Right——结果是否正确? B——是否所有的边界条件都是正确的? I——能查一下反响关联吗? C——能用其它手段交叉检查一下吗? E——你是否可以强制错误条件发生? P——是否满足性能要求? 测试计 ...
- mac 搭建Vue开发环境
1: 使用的各个工具的版本为: Homebrew 1node.js npm webpack Vue 2: 安装brew 打开终端运行一下命令 /usr/bin/ruby -e "$(cur ...
- PDB调试模块
这里主要是一些对于调试常用的命令:1.直接通过命令端输入进行调试 以pdb调试模式运行(主要用这个) python3 -m pdb file.py 2.在代码中导入pdb模块 import pdb 功 ...