[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 ...
随机推荐
- ExtJS模版技术
学习ExtJS一段时间以后,大家基本都会对于一些显示数据的组件不太符合需求,可能自己需要的组件在ExtJS里面不存在,这是大家基本就会使用Html属性,直接使用Html进行绘制页面数据展现. 但是,使 ...
- django 更新 模板语言
Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 在Django的模板语言中按此语法使用:{{ 变量名 ...
- 随着firefox的迭代更新:FireBug不能用了?使用火狐Try Xpath插件替代Firebug和Firepath
本篇文章讲解如何在火狐中安装和使用Try Xpath(插件替代Firebug和Firepath). 在火狐中安装Try Xpath 1. 打开火狐浏览器 FireFox57以上的版本 2. 在火狐菜单 ...
- 笔记本 T450的鼠标经常不灵
T450的鼠标经常不灵,鼠标总感觉有延迟. 换了鼠标也是这样. 有人反应说是USB断电, 使用的是省电模式,在设备管理>鼠标>电源选项>节电模式 勾去掉就可以了, 但是我的节点模式是 ...
- 使用示例带你提前了解 Java 9 中的新特性
使用示例带你提前了解 Java 9 中的新特性 转载来源:https://juejin.im/post/58c5e402128fe100603cc194 英文出处:https://www.journa ...
- socket 网络连接基础
socket 客户端 import socket 1.client = socket.socket() # socket.TCP/IP 选择连接的类型,默认为本地连接 2.client.connec ...
- centos7 jenkins 安装
前提: 安装了 jdk ,我的是jdk8 第一步: https://jenkins.io/download/ 下载 可以下载 rpm文件, 标红处, 也可以下载war包(Generic Java pa ...
- 控制html元素的隐藏问题
控制元素隐藏的方式,有display:none.visibility:hidden以及不透明度设置. 一.display:none 被隐藏的元素,在页面中不占位,空出的位置会被相邻的元素占用. < ...
- clamav杀毒软件的安装
Linux Unix.Trojan.Elknot (Linux.BackDoor.Gates.5)木马清理 此恶意软件结合了传统后门程序和DDoS攻击木马的功能 前两天性能测试服务器被种马,cpu一直 ...
- Becoming inspired - ASC - 2017 MARCH 24
Becoming inspired - The 11 questions to ask yourself when you feel uninspired @ Advanced Studio Clas ...