Leetcode392. Is Subsequence
Description
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.
My program:
从s和t的首端开始遍历比较字符是否相等即可,如果不等,则增加在t中的下标j位置;如果相等,则同时增加s中下标i和t中下标j。如果t中的指标位置增长到了t的末尾,而s中的指标还没有增长的末尾,则返回false。如果s中的指标先增长到了末尾,则返回true。
class Solution {
public:
bool isSubsequence(string s, string t) {
int i = 0, j = 0;
if (s.empty()) return true;
while(i<s.size() && j<t.size())
{
if(s[i] == t[j])
{
++i;
++j;
}
else
++j;
}
if (i<s.size()) return false;
else return true;
}
};
Simple C++ code:
bool isSubsequence(string s, string t) {
int si= 0;
for(int ti = 0; ti < t.size() && si < s.size(); ti++) {
if(t[ti] == s[si]) si++;
}
return si == s.size();
}
Leetcode392. Is Subsequence的更多相关文章
- [Swift]LeetCode392. 判断子序列 | 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] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [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] 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, ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- CF724D. Dense Subsequence[贪心 字典序!]
D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- 静态NAT地址转换
1.配置路由器端口ip(两个端口需要设置两个网段) Router(config)#inter f0/1 Router(confiog-if)#ip add 202.1.1.2 255.255.255. ...
- 每天一个linux命令12之top
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...
- mormot 数据集和JSON互相转换
mormot 数据集和JSON互相转换 uses SynVirtualDataSet, mORMotMidasVCL, SynCommons; procedure TForm1.Button1Clic ...
- docker_usb开发软件部署
1.docker镜像包 (备注:61提供,带桌面版本) rayosx2.0.2.tar 2.paho-mqtt dnf install git -y git clone https://github ...
- 算法导论-顺序统计-快速求第i小的元素
目录 1.问题的引出-求第i个顺序统计量 2.方法一:以期望线性时间做选择 3.方法二(改进):最坏情况线性时间的选择 4.完整测试代码(c++) 5.参考资料 内容 1.问题的引出-求第i个顺序统计 ...
- ExtJs 4中 Ext.Ajax.request提交实现waitMsg等待提示效果
//submitForm为form表单 var myMask = new Ext.LoadMask(Ext.getBody(),{msg:"请稍等,正在导入..."}); myMa ...
- 爬虫扒下 bilibili 视频信息
B站算是对爬虫非常非常友好的网站啦! 修改转载已取得腾讯云授权 在以上两篇文章中我们已经在腾讯云服务器上搭建好了 Python 爬虫环境了,下一步就是在云服务器上爬上我们的爬虫,抓取我们想要的数据: ...
- 字符串编码原理--PHP数组原理与高级应用
基础知识 1.有几种表达方式(查看手册)2.单引号和双引号的区别,双引号解析变量.\n,\t等,八进制与十六进制编码 内部存储方式 c语言中怎么表示字符串,结构体存储了字符指针和长度1.字符串可以用[ ...
- Alfresco 5.0.d 企业文档管理系统
Thanks for downloading Alfresco Community Edition. Your download should begin in three seconds. If i ...
- pytz 格式化北京时间 6分钟问题
使用datetime直接构造时间的时候,设置时区是没有北京时间的,一般来说习惯了linux的同志都会默认用上海时间来代替,这里却有一个问题,如果要进行时区转换,上海时间比北京时间差6分钟... 比如: ...