[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 ...
随机推荐
- Maven 添加jdk编译插件
问题描述: 默认情况下,eclipse的maven项目使用jdk1.5编译,而我们的jdk为1.8每次更改jdk1.5之后,只要maven项目已更新,eclipse就会自动的回到jdk1.8结局方法: ...
- 6.19-response(响应),session(会话技术,服务器端技术) 内置对象,application(内置对象),pageContext (内置对象),cookie(客户端技术)
一.response(响应) 页面重定向 response.sendRedirect(""); 转发: request.getRequestDispatcher("&qu ...
- Doris FE负载均衡配置
0 背景概述 Doris完全兼容了mysql协议,并且Doris FE本身通过多follower选举机制选举出master,可以保证fe本身的高可用性,也可以通过加入observer fe节点来提高f ...
- Linux--多网卡的7种Bond模式和交换机配置
网卡bond是通过把多张网卡绑定为一个逻辑网卡,实现本地网卡的冗余,带宽扩容和负载均衡.在应用部署中是一种常用的技术,我们公司基本所有的项目相关服务器都做了bond,这里总结整理,以便待查. bond ...
- Tensorflow图像操作
图像操作 图像基本概念 在图像数字化表示当中,分为黑白和彩色两种.在数字化表示图片的时候,有三个因素.分别是图片的长.图片的宽.图片的颜色通道数.那么黑白图片的颜色通道数为1,它只需要一个数字就可以表 ...
- 高负载均衡学习haproxy之安装与配置
https://www.cnblogs.com/ilanni/p/4750081.html
- Android 照相
XE6 控件太强了CameraComponent就可以了 CameraComponent1.Active := True; procedure TCameraComponentForm.CameraC ...
- Django之 Model Field Options
以下这些选项都是可选择的,非固定要求. 1)null,注意在CharField或者TextField里避免使用null,因为其存储的值是空字符串而不是NULL 2)blank该字段是否可以为空.如果为 ...
- IExpress 制作安装包 注意事项
被打包的文件名不能超过8个字符,否则iexpress会取前6个字符 + "~1".
- salt常用模块及API
saltstack提供了非常丰富的功能模块,涉及操作系统的基础功能,常用工具支持等,更多模块信息见官网模块介绍:https://docs.saltstack.com/en/latest/ref/mod ...