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 ...
随机推荐
- 关于RM中的X3014错误,以及mul() 、天空盒
关于 error X3014: incorrect number of arguments to numeric-type constructor 这个错误应该是某个类似float4 这样的变量初始 ...
- Emacs矩形操作
原始矩形块模式 emacs以C-x r开头的命令来进行矩形操作.先用C-space或者C-@设一个mark,移动光标到另一点,用以下命令进行列操作: C-x r r 复制一个矩形区域到寄存器 C-x ...
- android源码追踪学习 RecipientsEditor
RecipientsEditor 新建短信时输入收接者的editor, public class RecipientsEditor extends MultiAutoCompleteTextView ...
- xxx/labelKeypoint/utils/qt.py:81: RuntimeWarning: invalid value encountered in double_scalars
原代码: return np.linalg.norm(np.cross(p2 - p1, p1 - p3)) / np.linalg.norm(p2 - p1) 出现报错: xxx/labelKeyp ...
- tunning-prime优化mysql建议
#!/bin/sh # set tabstop=8 ################################################################## ...
- 《流畅的python》读书笔记
流畅的python 第1章 python数据模型 ---1.1 一摞Python风格的纸牌 特殊方法,即__method__,又被称为魔术方法(magic method)或者双下方法(dunder-m ...
- Robot Framework 自定义库
进入 python安装路径\Lib\site-packages 创建文件夹,库名 创建py文件,myclass.py 创建py文件,__init__.py 导入自定义库 遇到的问题: python版 ...
- linux中grep用法(“或”、“与”)
1.普通用法 grep keyword 若果keyword中有空格 grep "keyword" 例如: ls | grep mp4 2.“与”操作(其实就是多次筛选) grep ...
- zookeeper windows 下配置和基础命令
原文链接:http://blog.csdn.net/woshioosm/article/details/45560177 1, 解压zookeeper ,在目录下建立文件夹 data 和log 2,在 ...
- HDU 1002:A + B Problem II(大数相加)
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...