[LC] 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 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?
class Solution {
public boolean isSubsequence(String s, String t) {
if (s == null || s.length() == 0) {
return true;
}
int index = 0;
for (char ch : t.toCharArray()) {
if (index < s.length() && s.charAt(index) == ch) {
index += 1;
}
}
return index == s.length();
}
}
[LC] 392. Is Subsequence的更多相关文章
- 392. Is Subsequence
392. Is Subsequence 水题,先是判断长度,长度t比s小,返回false,然后从左到右扫描t,然后同时扫描s,如果相同,s的index就往后拉一个,如果s的index等于s长度,返回t ...
- 【LeetCode】392. Is Subsequence 解题报告(Python)
[LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...
- 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]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 ...
- 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
392. Is Subsequence Given a string s and a string t, check if s is subsequence of t. You may assume ...
- LeetCode_392. Is Subsequence
392. Is Subsequence Easy Given a string s and a string t, check if s is subsequence of t. You may as ...
随机推荐
- Ubuntu16.04编译tensorflow的C++接口
原文:https://www.bearoom.xyz/2018/09/27/ubuntu1604buildtf4cpp/ 之前有一篇介绍到在windows下利用VS2015编译tensorflow的C ...
- linux 解压命令总结
常用Linux 命令: [转自]https://www.jianshu.com/p/ca41f32420d6 解压缩 .tar 解包:tar xvf FileName.tar 打包:tar cvf F ...
- 刷题42. Trapping Rain Water
一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...
- HDU 2544 最短路 【Dijkstra模板题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...
- 将keras的h5模型转换为tensorflow的pb模型
h5_to_pb.py from keras.models import load_model import tensorflow as tf import os import os.path as ...
- JavaSE--Comparator 和 Comparable 区别
public interface Comparable<T> 此接口强行对实现它的每个类的对象进行整体排序.这种排序被称为类的自然排序,类的 compareTo 方法被称为它的自然比较方法 ...
- swoole使用内存
//swoole直接操作系统的内存 单线程每秒可执行三百万次 主要用于进程间的数据通信 $swoole_table = new swoole_table(1024);//1024为内创建内存对象所能存 ...
- 控制台输出<迷你DVD管理>
使用顺序.选择.循环.跳转语句 数组 功能实现菜单显示和切换 输入的数字不符合要求直接退出程序 用户可以选择新增.查看. 删除.借出.归还.退出 思路分析 使用switch语句实现菜单选择 使用do- ...
- 阿里云ECS搭建邮件服务
安装mailx [root@db ~]# yum install -y mailx [root@db ~]# vim /etc/mail.rc 设置发件人信息 ..... set from=yunwe ...
- iOS 一个新方法:- (void)makeObjectsPerformSelector:(SEL)aSelector;
NSArray 里面的一个方法, - (void)makeObjectsPerformSelector:(SEL)aSelector: 这是一个类似于执行for循环的方法,可以这样用,当需要删除一个v ...