Given a string s and a string t, check if s is subsequence of t.

You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).

Example 1:
s = "abc", t = "ahbgdc"

Return true.

Example 2:
s = "axc", t = "ahbgdc"

Return false.

Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?

Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.

 

这道题算比较简单的一种,我们可以用两个指针分别指向字符串s和t,然后如果字符相等,则i和j自增1,反之只有j自增1,最后看i是否等于s的长度,等于说明s已经遍历完了,而且字符都有在t中出现过,参见代码如下:

解法一:

class Solution {
public:
bool isSubsequence(string s, string t) {
int i = ;
for (int j = ; j < t.size() && i < s.size(); ++j) {
if (s[i] == t[j]) ++i;
}
return i == s.size();
}
};

题目中的 Follow up 说如果有大量的字符串s,问我们如何进行优化。那么既然字符串t始终保持不变,我们就可以在t上做一些文章。子序列虽然不需要是连着的子串,但是字符之间的顺序是需要的,那么我们可以建立字符串t中的每个字符跟其位置直接的映射,由于t中可能会出现重复字符,所以把相同的字符出现的所有位置按顺序加到一个数组中,所以就是用 HashMap 来建立每个字符和其位置数组之间的映射。然后遍历字符串s中的每个字符,对于每个遍历到的字符c,我们到 HashMap 中的对应的字符数组中去搜索,由于位置数组是有序的,我们使用二分搜索来加快搜索速度,这里需要注意的是,由于子序列是有顺序要求的,所以需要一个变量 pre 来记录当前匹配到t字符串中的位置,对于当前s串中的字符c,即便在t串中存在,但是若其在位置 pre 之前,也是不能匹配的。所以我们可以使用 uppper_bound() 来二分查找第一个大于 pre 的位置,若不存在,直接返回 false,否则将 pre 更新为二分查找的结果并继续循环即可,参见代码如下:

解法二:

// Follow up
class Solution {
public:
bool isSubsequence(string s, string t) {
int pre = -, n = t.size();
unordered_map<char, vector<int>> char2pos;
for (int i = ; i < n; ++i) char2pos[t[i]].push_back(i);
for (char c : s) {
auto it = upper_bound(char2pos[c].begin(), char2pos[c].end(), pre);
if (it == char2pos[c].end()) return false;
pre = *it;
}
return true;
}
};

类似题目:

Number of Matching Subsequences

参考资料:

https://leetcode.com/problems/is-subsequence/

https://leetcode.com/problems/is-subsequence/discuss/87302/Binary-search-solution-for-follow-up-with-detailed-comments

https://leetcode.com/problems/is-subsequence/discuss/87408/Binary-search-solution-to-cope-with-input-with-many-Ss(with-explanation)

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Is Subsequence 是子序列的更多相关文章

  1. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  2. LeetCode 376. Wiggle Subsequence 摆动子序列

    原题 A sequence of numbers is called a wiggle sequence if the differences between successive numbers s ...

  3. [LeetCode] 891. Sum of Subsequence Widths 子序列宽度之和

    Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the  ...

  4. [leetcode]392. Is Subsequence 验证子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  5. [LeetCode] Increasing Subsequences 递增子序列

    Given an integer array, your task is to find all the different possible increasing subsequences of t ...

  6. 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)

    引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...

  7. HDU 1159 Common Subsequence 公共子序列 DP 水题重温

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  8. 392 Is Subsequence 判断子序列

    给定字符串 s 和 t ,判断 s 是否为 t 的子序列.你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=10 ...

  9. [LeetCode]152. 乘积最大子序列(DP)

    题目 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. 示 ...

随机推荐

  1. 读书笔记--SQL必知必会13--创建高级联结

    13.1 使用表别名 SQL可以对列名.计算字段和表名起别名. 缩短SQL语句 允许在一条SELECT语句中多次使用相同的表. 注意:表别名只在查询执行中使用,不返回到客户端. MariaDB [sq ...

  2. 1元搭建自己的云服务器&解析域名

    最近在学做微信开发,没有自己的域名和服务器就不得不寄人篱下,索性自己就到云主机上搭建了个服务器,但是水平有限弄了一个下午~~有自己的域名和服务器的好处相信不用我多说了.比如日后可以有自己域名的个性博客 ...

  3. 类型基础---CLR Via C#笔记一

    一.所有类型都是从System.Obejct派生 1.下面两个类型定义是完全一致的: class Employee{ ... } class Employee:System.Object{ ... } ...

  4. fastjson-alibaba

    fastjson使用 package com.alibaba.json.demo; import org.junit.Assert; import com.alibaba.fastjson.JSON; ...

  5. C#开发微信门户及应用(9)-微信门户菜单管理及提交到微信服务器

    微信公众号(包括服务号和订阅号)都可以对菜单进行自定义设置,我们为了方便管理,一般先把菜单数据在本地管理维护,需要更新的时候,把它们更新到微信服务器上就可以了.本文基于这个方式,介绍我的微信门户平台管 ...

  6. MySQL动态游标

    通过(准备语句+视图+静态游标)实现 -- 建立测试表和数据 create table webuser (username varchar(10)); insert into webuser valu ...

  7. GJM : Unity3D HIAR -【 快速入门 】 六、导出 iOS 工程

    导出 iOS 工程 在开始之前,请务必先保存您的工程.由于 Unity 无法直接生成 ipa 文件,您需要先导出 iOS 工程文件,然后通过 Xcode 编译生成. Step 1. 选择平台 在 Un ...

  8. datatables中的Options总结(2)

    datatables中的Options总结(2) 五.datatable,列 columnDefs.targets 分配一个或多个列的列定义. columnDefs 设置列定义初始化属性. colum ...

  9. [deviceone开发]-do_Webview加载JQueryMobile的示例

    一.简介 JQueryMobile是JQuery的移动版,不过它并没有像JQuery那么成功.我们只是使用JQueryMobile来展示do_Webview加载第三方js框架.适合所有开发者.二.效果 ...

  10. 实现一个基于 SharePoint 2013 的 Timecard 应用(下)

    现在,基于 Timecard 数据来一点儿数据分析. 应用需求 对于 Timecard,分析下面 2 个方面: 对于单个项目,分析其中每个成员的工时占比,以此了解工作量分配,为组间人员调度提供参考. ...