LeetCode——Is Subsequence
Question
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.
Solution
依次匹配就好了。
Code
class Solution {
public:
bool isSubsequence(string s, string t) {
if (s.length() > t.length())
return false;
if (s.length() == 0)
return true;
int index = 0;
for (int i = 0; i < t.length(); i++) {
if (t[i] == s[index])
index++;
}
if (index == s.length())
return true;
else
return false;
}
};
LeetCode——Is Subsequence的更多相关文章
- [LeetCode] 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 ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- LeetCode "Wiggle Subsequence" !
Another interesting DP. Lesson learnt: how you define state is crucial.. 1. if DP[i] is defined as, ...
- [LeetCode] Is Subsequence 题解
前言 这道题的实现方法有很多,包括dp,贪心算法,二分搜索,普通实现等等. 题目 Given a string s and a string t, check if s is subsequence ...
- LeetCode "Is Subsequence"
There are 3 possible approaches: DP, divide&conquer and greedy. And apparently, DP has O(n^2) co ...
- leetcode 376Wiggle Subsequence
用dp解 1)up定义为nums[i-1] < nums[i] down nums[i-1] > nums[i] 两个dp数组, up[i],记录包含nums[i]且nums[i-1] & ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- Yii框架2.0的模块
模块是个独立的软件单元,也是又控制器,视图,模型组成的,也可以有自己的布局.但他必须属于某个应用下,不能独立存在. 其中模块的控制器,视图和应用的控制器和视图使用基本相同,不作赘述,下面说说模块的使用 ...
- linux下非root用户的sudo问题
linux下的root用户是个超级管理员,一般是不用这个用户登录进行操作的,但有时候需要root权限,又不想切换用户的话可以使用sudo命令.但是不是所有的用户都可以使用sudo命令的. 首先可能会遇 ...
- 转!mysql 命令行下 通过DELIMITER临时改变语句分隔符 执行存储过程
mysql 在 Navicat 界面工具 执行存储过程ok,但是在命令行下执行失败. 原因在于,默认的MySQL语句分隔符为' ; ',在输入' ; '的时候,“以为”语句已经结束了,但实际上语句还没 ...
- Django中间件如何处理请求
Django中间件 在http请求 到达视图函数之前 和视图函数return之后,django会根据自己的规则在合适的时机执行中间件中相应的方法. Django1.9版本以后中间件的执行流程 1. ...
- Secure Sockets Layer(安全套接层)
SSL SSL(Secure Sockets Layer安全套接层)及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议.TL ...
- 18.解决合并androidmanfest错误
这个是minsdk和tartgetsdk的版本不一致的问题
- Jenkins+Ant+Jmeter自动化测试平台
持续集成 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员每天至少集成一次,也就意味着每天可能会发生多次集成.每次集成都通过自动化的构建(包括编译,发布,自动 ...
- 创建Java不可变型的枚举类型Gender
创建Java不可变型的枚举类型,其实例如下: // 创建不可变型的枚举类 enum Gender { // 此处的枚举值必须调用对应的构造器来创建 MALE("男"), FEMAL ...
- py-统计一个矩阵中每一列的非0数的个数
1.文件类型类似于这样: 不过数据量比这个要更大一点. 2.对应上述数据的运行结果: import matplotlib.pyplot as plt with open('test.txt') as ...
- selenium 代理 Cookies 截图 等待 调用JS
改变用户代理 读取Cookies 调用Java Script Webdriver截图 页面等待 1. 改变用户代理 import org.junit.AfterClass; import org.ju ...