算法第八周 Is Subsequence
来源:互联网 发布:卡拉季奇 知乎 编辑:程序博客网 时间:2024/05/17 06:44
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?
思路:采用双指针,一次遍历即可,只要字符串t中含有s的所有元素即可。即在结束时判断指向s的指针是否指向末尾
class Solution {public: bool isSubsequence(string s, string t) { if(s.size()>t.size()) return false; int len1 = s.size(), len2 = t.size(), k1 = 0, k2 = 0; while(k1 < len1 && k2 < len2) { if(s[k1]==t[k2]) k1++; k2++; } return k1 == len1; }};
0 0
- 算法第八周 Is Subsequence
- 算法第八周Longest Increasing Subsequence[medium]
- 算法分析与设计第八周:300. Longest Increasing Subsequence
- 算法第八周Number of Longest Increasing Subsequence[medium]
- 2017算法课.09(Is Subsequence)
- 算法练习(23):Is Subsequence
- (算法分析Week13)Is Subsequence[Medium]
- 第八周:[Leetcode]516. Longest Palindromic Subsequence
- 第八周:[Leetcode]300. Longest Increasing Subsequence
- Is Subsequence
- Is Subsequence
- Is Subsequence
- Is Subsequence
- Is Subsequence
- Is Subsequence
- Is Subsequence
- Is Subsequence
- 第八周:(LeetCode 516) Longest Palindromic Subsequence(c++)
- cartographer源码浅析(一)-新鲜出炉
- windows Anaconda安装tensorflow出现的一些问题
- 了解SpringMVC框架及基本工作流程
- 微信企业号会议助手---微信JSSDK调用
- CF 754D 贪心 + 优先队列
- 算法第八周 Is Subsequence
- Swift
- SQLServer汉字转全拼音函数
- 排序算法四:交换排序之冒泡排序
- Solidity学习笔记(3)
- <!--[if IE 6]><![endif]-->等语句含义
- AE开发中、放大、缩小、漫游、全图的实现
- 最实用的常用css3知识点梳理二:过渡+2d变形+3d变形
- Redis缓存技术学习系列之事务处理