2017算法课.09(Is Subsequence)
来源:互联网 发布:芒果tv会员账号淘宝 编辑:程序博客网 时间:2024/06/06 19:45
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.
当t中找到匹配的字符时,迭代t,s的先行指数。
只要它到达t的末尾,或者我们发现s中的所有字符,停止循环。
循环后,如果s的指数等于其长度,则返回true。 否则返回false。
bool isSubsequence(string s, string t) {
int sLength = s.length(), sIndex = 0, tLength = t.length();
for (int i=0; i<tLength && sIndex<sLength; i++)
if (t[i]==s[sIndex]) sIndex++;
return sIndex==sLength;
}
- 2017算法课.09(Is Subsequence)
- 算法第八周 Is Subsequence
- 算法练习(23):Is Subsequence
- (算法分析Week13)Is Subsequence[Medium]
- Is Subsequence
- Is Subsequence
- Is Subsequence
- Is Subsequence
- Is Subsequence
- Is Subsequence
- Is Subsequence
- Is Subsequence
- 【Leetcode】Is Subsequence
- 392. Is Subsequence
- [392]Is Subsequence
- 392.[LeetCode] Is Subsequence
- 392. Is Subsequence
- 392. Is Subsequence
- 数据语句
- AngularJs学习笔记(1)--简介和数据绑定
- 合并排序的递归算法
- 浏览器渲染引擎
- 推荐系统之协同过滤(CF)算法
- 2017算法课.09(Is Subsequence)
- vb.net 教程 3-3 窗体编程 输入对话框 2
- 二分搜索树的实现以及各种操作(支持重复节点)
- 【Java】大文本字符串滤重的简单方案~
- AngularJs学习笔记(2)--控制器和ng-bind
- 用户画像设计总结
- 三十七将项目四 大奖赛计分
- Linux内核学习总结
- 事务的传播行为(讲得比较好)