Skip to content

Commit 917e088

Browse files
committed
feat: add solutions to lc problem: No.0392
No.0392.Is Subsequence
1 parent 1140934 commit 917e088

File tree

7 files changed

+112
-62
lines changed

7 files changed

+112
-62
lines changed

solution/0300-0399/0392.Is Subsequence/README.md

+39-20
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050

5151
**方法一:双指针**
5252

53+
我们定义两个指针 $i$ 和 $j$,分别指向字符串 $s$ 和 $t$ 的初始位置。每次我们比较两个指针指向的字符,如果相同,则两个指针同时右移;如果不同,则只有 $j$ 右移。当指针 $i$ 移动到字符串 $s$ 的末尾时,说明 $s$ 是 $t$ 的子序列。
54+
55+
时间复杂度 $O(m + n)$,其中 $m$ 和 $n$ 分别是字符串 $s$ 和 $t$ 的长度。空间复杂度 $O(1)$。
56+
5357
<!-- tabs:start -->
5458

5559
### **Python3**
@@ -59,12 +63,12 @@
5963
```python
6064
class Solution:
6165
def isSubsequence(self, s: str, t: str) -> bool:
62-
i, j, m, n = 0, 0, len(s), len(t)
63-
while i < m and j < n:
66+
i = j = 0
67+
while i < len(s) and j < len(t):
6468
if s[i] == t[j]:
6569
i += 1
6670
j += 1
67-
return i == m
71+
return i == len(s)
6872
```
6973

7074
### **Java**
@@ -95,9 +99,10 @@ public:
9599
bool isSubsequence(string s, string t) {
96100
int m = s.size(), n = t.size();
97101
int i = 0, j = 0;
98-
while (i < m && j < n) {
99-
if (s[i] == t[j]) ++i;
100-
++j;
102+
for (; i < m && j < n; ++j) {
103+
if (s[i] == t[j]) {
104+
++i;
105+
}
101106
}
102107
return i == m;
103108
}
@@ -123,15 +128,15 @@ func isSubsequence(s string, t string) bool {
123128

124129
```ts
125130
function isSubsequence(s: string, t: string): boolean {
126-
let m = s.length,
127-
n = t.length;
131+
const m = s.length;
132+
const n = t.length;
128133
let i = 0;
129-
for (let j = 0; j < n && i < m; ++j) {
130-
if (s.charAt(i) == t.charAt(j)) {
134+
for (let j = 0; i < m && j < n; ++j) {
135+
if (s[i] === t[j]) {
131136
++i;
132137
}
133138
}
134-
return i == m;
139+
return i === m;
135140
}
136141
```
137142

@@ -160,19 +165,33 @@ impl Solution {
160165
### **C**
161166

162167
```c
163-
bool isSubsequence(char *s, char *t) {
164-
int n = strlen(s);
168+
bool isSubsequence(char * s, char * t){
169+
int m = strlen(s);
170+
int n = strlen(t);
165171
int i = 0;
166-
for (int j = 0; j < n; j++) {
167-
while (t[i] && t[i] != s[j]) {
168-
i++;
172+
for (int j = 0; i < m && j < n; ++j) {
173+
if (s[i] == t[j]) {
174+
++i;
169175
}
170-
if (!t[i]) {
171-
return 0;
176+
}
177+
return i == m;
178+
}
179+
```
180+
181+
### **C#**
182+
183+
```cs
184+
public class Solution {
185+
public bool IsSubsequence(string s, string t) {
186+
int m = s.Length, n = t.Length;
187+
int i = 0, j = 0;
188+
for (; i < m && j < n; ++j) {
189+
if (s[i] == t[j]) {
190+
++i;
191+
}
172192
}
173-
i++;
193+
return i == m;
174194
}
175-
return 1;
176195
}
177196
```
178197

solution/0300-0399/0392.Is Subsequence/README_EN.md

+41-20
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,25 @@
3030

3131
## Solutions
3232

33+
**Approach 1: Two Pointers**
34+
35+
We define two pointers $i$ and $j$ to point to the initial position of the string $s$ and $t$ respectively. Each time we compare the two characters pointed to by the two pointers, if they are the same, both pointers move right at the same time; if they are not the same, only $j$ moves right. When the pointer $i$ moves to the end of the string $s$, it means that $s$ is the subsequence of $t$.
36+
37+
The time complexity is $O(m + n)$, where $m$ and $n$ are the lengths of the strings $s$ and $t$ respectively. The space complexity is $O(1)$.
38+
3339
<!-- tabs:start -->
3440

3541
### **Python3**
3642

3743
```python
3844
class Solution:
3945
def isSubsequence(self, s: str, t: str) -> bool:
40-
i, j, m, n = 0, 0, len(s), len(t)
41-
while i < m and j < n:
46+
i = j = 0
47+
while i < len(s) and j < len(t):
4248
if s[i] == t[j]:
4349
i += 1
4450
j += 1
45-
return i == m
51+
return i == len(s)
4652
```
4753

4854
### **Java**
@@ -71,9 +77,10 @@ public:
7177
bool isSubsequence(string s, string t) {
7278
int m = s.size(), n = t.size();
7379
int i = 0, j = 0;
74-
while (i < m && j < n) {
75-
if (s[i] == t[j]) ++i;
76-
++j;
80+
for (; i < m && j < n; ++j) {
81+
if (s[i] == t[j]) {
82+
++i;
83+
}
7784
}
7885
return i == m;
7986
}
@@ -99,15 +106,15 @@ func isSubsequence(s string, t string) bool {
99106

100107
```ts
101108
function isSubsequence(s: string, t: string): boolean {
102-
let m = s.length,
103-
n = t.length;
109+
const m = s.length;
110+
const n = t.length;
104111
let i = 0;
105-
for (let j = 0; j < n && i < m; ++j) {
106-
if (s.charAt(i) == t.charAt(j)) {
112+
for (let j = 0; i < m && j < n; ++j) {
113+
if (s[i] === t[j]) {
107114
++i;
108115
}
109116
}
110-
return i == m;
117+
return i === m;
111118
}
112119
```
113120

@@ -136,19 +143,33 @@ impl Solution {
136143
### **C**
137144

138145
```c
139-
bool isSubsequence(char *s, char *t) {
140-
int n = strlen(s);
146+
bool isSubsequence(char * s, char * t){
147+
int m = strlen(s);
148+
int n = strlen(t);
141149
int i = 0;
142-
for (int j = 0; j < n; j++) {
143-
while (t[i] && t[i] != s[j]) {
144-
i++;
150+
for (int j = 0; i < m && j < n; ++j) {
151+
if (s[i] == t[j]) {
152+
++i;
145153
}
146-
if (!t[i]) {
147-
return 0;
154+
}
155+
return i == m;
156+
}
157+
```
158+
159+
### **C#**
160+
161+
```cs
162+
public class Solution {
163+
public bool IsSubsequence(string s, string t) {
164+
int m = s.Length, n = t.Length;
165+
int i = 0, j = 0;
166+
for (; i < m && j < n; ++j) {
167+
if (s[i] == t[j]) {
168+
++i;
169+
}
148170
}
149-
i++;
171+
return i == m;
150172
}
151-
return 1;
152173
}
153174
```
154175

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
bool isSubsequence(char* s, char* t) {
2-
int n = strlen(s);
1+
bool isSubsequence(char * s, char * t){
2+
int m = strlen(s);
3+
int n = strlen(t);
34
int i = 0;
4-
for (int j = 0; j < n; j++) {
5-
while (t[i] && t[i] != s[j]) {
6-
i++;
5+
for (int j = 0; i < m && j < n; ++j) {
6+
if (s[i] == t[j]) {
7+
++i;
78
}
8-
if (!t[i]) {
9-
return 0;
10-
}
11-
i++;
129
}
13-
return 1;
14-
}
10+
return i == m;
11+
}

solution/0300-0399/0392.Is Subsequence/Solution.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ class Solution {
33
bool isSubsequence(string s, string t) {
44
int m = s.size(), n = t.size();
55
int i = 0, j = 0;
6-
while (i < m && j < n) {
7-
if (s[i] == t[j]) ++i;
8-
++j;
6+
for (; i < m && j < n; ++j) {
7+
if (s[i] == t[j]) {
8+
++i;
9+
}
910
}
1011
return i == m;
1112
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Solution {
2+
public bool IsSubsequence(string s, string t) {
3+
int m = s.Length, n = t.Length;
4+
int i = 0, j = 0;
5+
for (; i < m && j < n; ++j) {
6+
if (s[i] == t[j]) {
7+
++i;
8+
}
9+
}
10+
return i == m;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def isSubsequence(self, s: str, t: str) -> bool:
3-
i, j, m, n = 0, 0, len(s), len(t)
4-
while i < m and j < n:
3+
i = j = 0
4+
while i < len(s) and j < len(t):
55
if s[i] == t[j]:
66
i += 1
77
j += 1
8-
return i == m
8+
return i == len(s)
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
function isSubsequence(s: string, t: string): boolean {
2-
let m = s.length,
3-
n = t.length;
2+
const m = s.length;
3+
const n = t.length;
44
let i = 0;
5-
for (let j = 0; j < n && i < m; ++j) {
6-
if (s.charAt(i) == t.charAt(j)) {
5+
for (let j = 0; i < m && j < n; ++j) {
6+
if (s[i] === t[j]) {
77
++i;
88
}
99
}
10-
return i == m;
10+
return i === m;
1111
}

0 commit comments

Comments
 (0)