当前位置:网站首页>LeetCode_392_判断子序列
LeetCode_392_判断子序列
2022-07-24 23:53:00 【Fitz1318】
题目链接
题目描述
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
进阶:
如果有大量输入的 S,称作 S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?
致谢:
特别感谢 @pbrother 添加此问题并且创建所有测试用例。
示例 1:
输入:s = "abc", t = "ahbgdc"
输出:true
示例 2:
输入:s = "axc", t = "ahbgdc"
输出:false
提示:
0 <= s.length <= 1000 <= t.length <= 10^4- 两个字符串都只由小写字符组成。
解题思路
双指针法
fast指针指向字符串tslow指针指向字符串s- 如果
t.charAt(fast) == s.charAt(slow),那么fast++,slow++ - 否则就只是
fast++ - 等遍历完字符串
t后判断slow == s.length()
AC代码
class Solution {
public boolean isSubsequence(String s, String t) {
int fast = 0;
int slow = 0;
while (slow < s.length() && fast < t.length()) {
if (s.charAt(slow) == t.charAt(fast)) {
slow++;
fast++;
} else {
fast++;
}
}
return slow == s.length();
}
}
边栏推荐
- 多线程&高并发(全网最新:面试题 + 导图 + 笔记)面试手稳心不慌
- Implementation of cat and dog data set classification experiment based on tensorflow and keras convolutional neural network
- Live broadcast preview | online seminar on open source security governance models and tools
- 采坑记录:TypeError: 'module' object is not callable
- Use of serial queues
- Processing PDF and JPG files in VB6
- Notes of Teacher Li Hongyi's 2020 in-depth learning series 3
- codeforces round #805 ABCDEFG
- [brother hero July training] day 20: search Binary Tree
- HTB-Aragog
猜你喜欢

基于Verilog HDL的数字秒表

JS ------ Chapter 5 functions and events

Live broadcast preview | online seminar on open source security governance models and tools

凸优化基础知识

Notes of Teacher Li Hongyi's 2020 in-depth learning series 6

Paper notes: accurate causal influence on discrete data

Notes of Teacher Li Hongyi's 2020 in-depth learning series 5

Go基础笔记_4_map

Sql文件导入数据库-保姆级教程

With screen and nohup running, there is no need to worry about deep learning code anymore | exiting the terminal will not affect the operation of server program code
随机推荐
WP wechat export chat record backup to computer
Which securities account is the best and safest for beginners
Qt学习-利用数据库单例完成 登录匹配 + 注册 功能实现
Processing of ffmpeg wasapi can't activate audio endpoint error
Heap sort summary
Zheng Huijuan: Research on application scenarios and evaluation methods of data assets based on the unified market
1、 MFC introduction
Upload and download filask files
Financial products can reach 6%. I want to open an account to buy financial products
Analyzing the principle of DNS resolution in kubernetes cluster
Digital stopwatch based on Verilog HDL
yolov5
Install K6 test tool
Go基础笔记_4_map
[nuxt 3] (x) runtime configuration
Opengauss kernel analysis: query rewriting
线段树杂谈
Grafana - influxdb visual K6 output
.net redis client newlife.redis.core library usage
Remember the problem of using redisson to step on the pit once