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, ...
随机推荐
- ubuntu android 设备识别 Setting up a Device for Development
参考: http://developer.android.com/tools/device.html lsusb Bus 001 Device 004: ID 18d1:9025 Google I ...
- 设计模式之——Builder建造模式
Builder模式又叫建造模式,是用于组装具有复杂结构的实例的模式. 示例程序是编写一个文档,并且写入到文件中,该文档具有以下结构,含有标题,字符串,一些条目. Builder抽象类,为建造模式的核心 ...
- Django接收自定义http header(转)
add by zhj: Django将所有http header(包括你自定义的http header)都放在了HttpRequest.META这个Python标准字典中,当然HttpRequest. ...
- JavaWeb-Servlet-通过servlet生成验证码图片
BufferedImage类 创建一个BufferImage servlet,用来生成验证码图片: package com.fpc; import java.awt.Color; import jav ...
- yum源的报错排除
echo "http://vault.centos.org/6.5/os/x86_64/" > /var/cache/yum/x86_64/6/base/mirrorlist ...
- HDU1003:Max Sum(简单dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题意一目了然就不说了,算法是从左往右扫,一个暂时保存结果的值,如果区间结果<0,那么就更改左右 ...
- Java应用程序中System.out.println输出中文乱码
其实,解决办法比较简单,即:编译时指定编码为UTF-8,如: javac -encoding utf- HelloJava.java 这样,再运行时就不会出现乱码. 比较详细的内容可以参考:http: ...
- python中的内置函数总结
官方文档 一. 数学函数 #abs() 绝对值 #bin() 二进制 0b #oct() 八进制 0o #hex() 十六进制 0x #complex 复数 x=1-2j print(x) print ...
- 160726 smarty 笔记(2)
<?php //取当前页 $p=1; if(!empty($_GET["page"])) { $p=$_GET["page"]; } //定义页面缓存文件 ...
- C++与C混编
C++与C混编 本案例通过实现一个简单的UDP服务器来说明C++与C的混合编程问题 C代码 通过C代码来对UDP服务器的创建,监听进行封装 udp.c文件 #include <sys/types ...