[leetcode]392. Is Subsequence 验证子序列
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 sis 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?
题意:
给定串串s和t, 判断s是否是t的subsequence
思路:
指针count用来scan串串s
指针i用来scan串串t
从左至右,若t当前的字符等于s当前字符,count++
若count等于s.length(),说明s中的每个字符都包含在串串t中,最终说明s是t的subsequence
代码:
class Solution {
public boolean isSubsequence(String s, String t) {
if(s == null || s.length() == 0) return true;
int count = 0;
for(int i = 0; i<t.length() ; i++){
if(t.charAt(i) == s.charAt(count)){
count++;
if(count == s.length()){return true;}
}
}
return false;
}
}
[leetcode]392. Is Subsequence 验证子序列的更多相关文章
- LeetCode 392. 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 376. Wiggle Subsequence 摆动子序列
原题 A sequence of numbers is called a wiggle sequence if the differences between successive numbers s ...
- 392 Is Subsequence 判断子序列
给定字符串 s 和 t ,判断 s 是否为 t 的子序列.你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=10 ...
- [leetcode] 392. Is Subsequence (Medium)
原题 判断子序列 /** * @param {string} s * @param {string} t * @return {boolean} */ var isSubsequence = func ...
- LeetCode 392. Is Subsequence 详解
题目详情 给定字符串 s 和 t ,判断 s 是否为 t 的子序列. 你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 & ...
- 【LeetCode】392. Is Subsequence 解题报告(Python)
[LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 392. Is Subsequence
392. Is Subsequence 水题,先是判断长度,长度t比s小,返回false,然后从左到右扫描t,然后同时扫描s,如果相同,s的index就往后拉一个,如果s的index等于s长度,返回t ...
- LeetCode:递增的三元子序列【334】
LeetCode:递增的三元子序列[334] 题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i ...
随机推荐
- tornado.gen 模块解析
转自:http://strawhatfy.github.io/2015/07/22/Tornado.gen/ 引言 注:正文中引用的 Tornado 代码除特别说明外,都默认引用自 Tornado 4 ...
- 10-17(day2)
这次写day2的总结 T1:表达式 题面:给你一串表达式 在本题中,我们对合法表达式定义如下:1. 任何连续(至少1个)数字是合法表达式:2. 若x是合法表达式,则(x)也是合法表达式:3. 若x和y ...
- php基础和数据库
服务器和客户端 客户端 程序: 通过浏览器直接运行 服务器 程序: 通过安装某种服务器软件 程序才可以运行 apache php文件 tom ...
- Python入门一:简单得不能再简单了##
从python的语法上看,简单得不能再简单了. 想学它,请移步廖雪峰python2.7教程以及python3.这实在是最好的入门教程.参考资料太多: 外国的教程 Python 入门指南 Python ...
- MONGODB用户、角色和权限管理
最近遇到很多问MONGODB用户认证授权方面的问题,现在特记录下来,与大家共享. 一.概念理解 1.用户概念 Mongodb的用户是由 用户名+所属库名组成 例如: 登录mongo testdb ...
- python 字体颜色的设置
实现过程: 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关. 转义序列是以ESC开头,即用\033来完成(ESC的ASCII码用十进制表示是27 ...
- [Flutter] 发布自己的插件 package
我们自己做了插件包,当然也想发布到pub.dartlang.org上去.怎么发布呢?先看看官方的说明(https://flutter.io/developing-packages/). Publish ...
- shell for的使用
好久没写shell了,花了半个小时才写对,不常用果然不够熟悉,以后还是得每天花半个小时写写shell #!/bin/sh #set -x p="/" cd $p echo $(pw ...
- oracle创建表空间,表及用户
oracle要创建表要首先创建表空间,当然默认是有表空间的.而mysql创建表时,会自动创建表空间,myisam会自动建三个文 件.MYD,.MYI,.frm.innodb呢,如果没有配置独立表空间的 ...
- 代码:css小图标
向下小箭头 .icon-tip{ border-color: transparent transparent #bb0808 transparent; border-style:solid; bord ...