Subsequence - 392. Is Subsequence
- 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?
Credits: Special thanks to @pbrother for adding this problem and creating all test cases.
思路:
用贪心的思想,找出s串中第一个在t中出现的位置。
代码:
java:
class Solution {
public boolean isSubsequence(String s, String t) {
if (s == null || s.length() == 0) return true;
if (t == null || t.length() == 0) return false;
/* int index = 0;
for (int i = 0; i < t.length(); i++) {
if (s.charAt(index) == t.charAt(i)) index++;
if (index == s.length()) return true;
}
return false;*/
int prev = -1;
for (int i = 0; i < s.length(); i++) {
int index = t.indexOf(s.charAt(i), prev+1);
if (index < 0) {
return false;
}
prev = index;
}
return true;
}
}
- Linux系统下的ssh使用(依据个人经验总结)
- 从MapX到MapXtreme2004[4]-标注AutoLabel
- Linux下锁定账号,禁止登录系统的设置总结
- 深度解析 TypeConverter & TypeConverterAttribute (一)
- 从MapX到MapXtreme2004[7]-对Table、Feature等的理解
- 互联网赋能传统装企 “科技撬动力巨大”
- Python接口自动化-8-测试报告
- http应用优化和加速说明-负载均衡
- linux负载均衡总结性说明(四层负载/七层负载)
- 从MapX到MapXtreme2004[6]-标点心得
- silverlight3中的"伪"3D
- 暴利驱动的疯狂游戏“外挂”:非法获利可达数百万;X-Agent 后门大升级,俄罗斯 APT28 间谍活动更为隐蔽
- Nginx+keepalived双机热备(主从模式)
- 即使不做程序员,也要学会像程序员一样去思考
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Vue Nginx反向代理配置 解决生产环境跨域
- react的事件处理为什么要bind this 改变this的指向?
- 没有用到React,为什么我需要import引入React?
- JSON对象和JavaScript对象直接量的区别--不同之处
- (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
- 关于DOM的理解
- 关于闭包函数和递归函数的详细理解
- 新手怎么学JS?JavaScript基础入门
- 纯CSS制作加<div>制作动画版哆啦A梦
- Ubuntu 18.04 安装使用 Supervisor 进程守护并设置开机自动启动
- AWS CLI version 2 在 Windows 下的安装
- Vue事件绑定原理
- 组合
- 用navicat进行身份验证连接出现cannot connect to Mongodb authentication failed
- three.js中的矩阵计算