Skip to content

Commit 4da4433

Browse files
committed
二刷392
1 parent d9cb5d3 commit 4da4433

File tree

4 files changed

+66
-38
lines changed

4 files changed

+66
-38
lines changed

docs/0392-is-subsequence.adoc

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,47 @@
11
[#0392-is-subsequence]
2-
= 392. Is Subsequence
2+
= 392. 判断子序列
33

4-
{leetcode}/problems/is-subsequence/[LeetCode - Is Subsequence^]
4+
https://leetcode.cn/problems/is-subsequence/[LeetCode - 392. 判断子序列 ^]
55

6+
给定字符串 *s**t* ,判断 *s* 是否为 *t* 的子序列。
67

7-
Given a string *s* and a string *t*, check if *s* is subsequence of *t*.
8+
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,`"ace"``"abcde"`的一个子序列,而`"aec"`不是)。
89

10+
*进阶:*
911

12+
如果有大量输入的 S,称作 S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?
1013

11-
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).
14+
*致谢:*
1215

16+
特别感谢 ** https://leetcode.com/pbrother/[@pbrother] 添加此问题并且创建所有测试用例。
1317

18+
*示例 1:*
1419

15-
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).
20+
....
21+
输入:s = "abc", t = "ahbgdc"
22+
输出:true
23+
....
1624

25+
*示例 2:*
1726

18-
*Example 1:*
27+
....
28+
输入:s = "axc", t = "ahbgdc"
29+
输出:false
30+
....
1931

32+
*提示:*
2033

21-
*s* = `"abc"`, *t* = `"ahbgdc"`
22-
23-
24-
Return `true`.
25-
26-
27-
*Example 2:*
28-
29-
30-
*s* = `"axc"`, *t* = `"ahbgdc"`
31-
32-
33-
Return `false`.
34-
35-
36-
*Follow up:*
37-
38-
39-
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?
40-
41-
*Credits:*
42-
43-
Special thanks to <a href="https://leetcode.com/pbrother/">@pbrother</a> for adding this problem and creating all test cases.
34+
* `+0 <= s.length <= 100+`
35+
* `0 \<= t.length \<= 10^4^`
36+
* 两个字符串都只由小写字符组成。
4437
4538
4639
== 思路分析
4740

4841
双指针,当短指针到头也就是表明是子串。
4942

43+
image::images/0392-10.gif[{image_attr}]
44+
5045
[[src-0392]]
5146
[tabs]
5247
====
@@ -59,17 +54,18 @@ include::{sourcedir}/_0392_IsSubsequence.java[tag=answer]
5954
----
6055
--
6156
62-
// 二刷::
63-
// +
64-
// --
65-
// [{java_src_attr}]
66-
// ----
67-
// include::{sourcedir}/_0392_IsSubsequence_2.java[tag=answer]
68-
// ----
69-
// --
57+
二刷::
58+
+
59+
--
60+
[{java_src_attr}]
61+
----
62+
include::{sourcedir}/_0392_IsSubsequence_2.java[tag=answer]
63+
----
64+
--
7065
====
7166

67+
7268
== 参考资料
7369

74-
. https://leetcode.cn/problems/is-subsequence/solutions/1658262/by-jyd-zeph/?envType=study-plan-v2&envId=selected-coding-interview[392. 判断子序列 - 双指针,清晰图解^]
70+
. https://leetcode.cn/problems/is-subsequence/solutions/1658262/by-jyd-zeph/[392. 判断子序列 - 双指针,清晰图解^]
7571

docs/images/0392-10.gif

8.66 MB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,11 @@ endif::[]
668668
|{doc_base_url}/0350-intersection-of-two-arrays-ii.adoc[题解]
669669
|✅ 最初思路是对两个数组统计次数。其实只需要对一个数组统计次数,另外一个数组遍历时就可以求交集了。
670670

671+
|{counter:codes2503}
672+
|{leetcode_base_url}/is-subsequence/[392. 判断子序列^]
673+
|{doc_base_url}/0392-is-subsequence.adoc[题解]
674+
|✅ 双指针
675+
671676
|===
672677

673678
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0392_IsSubsequence_2 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-05-13 20:18:27
9+
*/
10+
public boolean isSubsequence(String s, String t) {
11+
if (s == null || s.isEmpty()) {
12+
return true;
13+
}
14+
if (t == null || t.isEmpty() || s.length() > t.length()) {
15+
return false;
16+
}
17+
int m = 0, n = 0;
18+
while (m < s.length() && n < t.length()) {
19+
if (s.charAt(m) == t.charAt(n)) {
20+
m++;
21+
}
22+
n++;
23+
}
24+
return m == s.length();
25+
}
26+
// end::answer[]
27+
}

0 commit comments

Comments
 (0)