Find minimum continuous subsequence tags
Given targetList, a list of strings representing the desired tags, and availableTagList, a list of strings representing the sequence of all available tags. Find the start index and end index of the minimus continuous subsequence containing all the desired tags in any order. If more that one results, return the one with the smaller start index.
Note:
- No dup tags in targetList.
- Returned subsequence may include additional tags beyond those in targetList.
- Consider only alphanumeric characters.
- Output 0 if no match.
- 字符串中等题。Sliding window algorithem + Hash。
- 思想跟其他滑动窗口题一样,不同的是此题没有给出window size,而是要找最小window。还是对targetList建立hash表,然后扩展window end point去找到能包含所有targetList tags的window。找到之后,再移动window start point。这里跟其他题目不一样的地方是,window size并不固定,而且需要找最小window size,所以在满足count == 0条件下,扩展start point来缩小window,直到当前window已经不能包含所有tags。
//
// 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<int> subSequenceTags(vector<string> targetList, vector<string> availableTagList) {
vector<int> vResult; // corner case
if (targetList.empty() || availableTagList.empty()) {
vResult.push_back();
return vResult;
} unordered_map<string, int> hash; for (auto & it : targetList)
++ hash[it]; // window start/end point, hit count ( sum { positive hash[i] } ), min result length, result start index
int left = , right = , count = targetList.size(), len = INT_MAX, start = ; while (right < availableTagList.size()) {
if (hash[availableTagList.at(right)] > ) // could use == 1 as well coz that no dup in targetList
-- count; -- hash[availableTagList.at(right)]; ++ right; // continue move window start point rightward to minimize window until condition not valid
while ( == count) {
if (right - left < len) {
len = right - left;
start = left;
} if (hash[availableTagList.at(left)] >= ) // could change the condition to == 0 if condition for -- count varies
++ count; ++ hash[availableTagList.at(left)]; ++ left;
}
} if (len != INT_MAX) {
vResult.push_back(start);
vResult.push_back(start + len - );
} else
vResult.push_back(); // Don't miss the empty result if no match return vResult;
}
}; int main(int argc, char* argv[])
{
Solution testSolution; vector<vector<string>> tInputs = {{"made", "in", "spain"}, {"2abc","bcd", "cab"}, {"in", "the", "spain"}, {"in"}, {}, {"in"}};
vector<vector<string>> aInputs = {{"made", "weather", "forecast", "says", "that", "made", "rain", "in", "spain", "stays"},
{"dbc", "2abc", "cab", "bcd", "bcb"},
{"the", "spain", "that", "the", "rain", "in", "spain", "stays", "forecast", "in", "the"},
{"aa", "bb", "cc"},
{"aa", "bb", "cc"},
{}};
vector<int> result; /*
5 8
1 3
3 6
0
0
0
*/
for (auto i = ; i < tInputs.size(); ++ i) {
result = testSolution.subSequenceTags(tInputs[i], aInputs[i]); for (auto it : result)
cout << it << " ";
cout << endl;
} return ;
}
Find minimum continuous subsequence tags的更多相关文章
- [LeetCode] Minimum Window Subsequence 最小窗口序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...
- [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列
Give an integer array,find the longest increasing continuous subsequence in this array. An increasin ...
- Lintcode397 Longest Increasing Continuous Subsequence solution 题解
[题目描述] Give an integer array,find the longest increasing continuous subsequence in this array. An in ...
- [LintCode] Longest Increasing Continuous subsequence
http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/# Give you an integer a ...
- LintCode 397: Longest Increasing Continuous Subsequence
LintCode 397: Longest Increasing Continuous Subsequence 题目描述 给定一个整数数组(下标从0到n - 1,n表示整个数组的规模),请找出该数组中 ...
- LC 727. Minimum Window Subsequence 【lock,hard】
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- LintCode "Longest Increasing Continuous subsequence II" !!
DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vecto ...
随机推荐
- spring的orm模块
spring整合hibernate 1.hibernate使用注解. daoImpl需要继承HibernateDaoSupport对象,针对给对象的getHibernateTemplate()进行hi ...
- JDBC事务控制管理(转载)
JDBC事务控制管理 转载于 2018年01月26日 15:46:11 1.事务 (1)事务的概念 事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 例如:A——B转帐, ...
- \n,\r,\t
etF首先说说\n,\r,\t \n 软回车: 在Windows 中表示换行且回到下一行的最开始位置 在Linux.unix 中只表示换行,但不会回到下一行的开始位置. \r 软空格: 在Linux. ...
- 解决Android4.3版本下,手机短彩接收中文文件名附件,中文名字的附件无法保存(第二步:解决从从数据库中读取附件文件名,并在长按后保存附件时,中文乱码导致的无法保存附件)
从第一步我们发现,在第一步修改之后,在短彩绘画界面中中文附件名的附件已无法显示,经过打印堆栈我们发现还是中文乱码在作祟.下面我们接着进行分析,这次我们从UI层往逻辑处理层进行分析.首先我们找到保存附件 ...
- 3.3 shell控制流结构
shell中的控制流包括if then else语句,case语句,for循环,until循环,while循环,break控制,continue控制. 条件测试: 有时判断字符串是否相等或检查文件状态 ...
- C++ 泛型 编写的 数据结构 栈
平时编程里经常需要用到数据结构,比如 栈和队列 等, 为了避免每次用到都需要重新编写的麻烦现将 C++ 编写的 数据结构 栈 记录下来,以备后用. 将 数据结构 栈 用头文件的形式 ...
- STM32 输入捕获配置
在STM32 的定时器,除了 TIM6 和 TIM7,就是通过检测 TIMx_CHx 上的 边沿信号,在边沿信号发生跳变(比如上升沿/下降沿)的时候, 将当时定时器 的值(TIMx_CNT) 存放到对 ...
- matplotlib的颜色及线条控制
refer to: https://www.cnblogs.com/darkknightzh/p/6117528.html
- elastic search 日期为string类型导致视图无法展示时间的解决办法
尝试将结构化的json数据发送到es(elastic search)上,然后创建视图,这样就能以小时维度查看数据,直接使用post发送到es后,创建索引,结果提示 没有date类型的字段(field) ...
- streamsets 集成 minio s3测试
具体streamsets crate 集成可以参考 streamsets crate 以下文档只关注minio 集成的配置 minio 服务 搭建 具体搭建参考: https://www.cnblog ...