Skip to content

Commit fba5186

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0392
1 parent 30a99b9 commit fba5186

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

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

+24-2
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,44 @@
3333

3434
<!-- 这里可写通用的实现逻辑 -->
3535

36+
双指针遍历即可。
37+
3638
<!-- tabs:start -->
3739

3840
### **Python3**
3941

4042
<!-- 这里可写当前语言的特殊实现逻辑 -->
4143

4244
```python
43-
45+
class Solution:
46+
def isSubsequence(self, s: str, t: str) -> bool:
47+
m, n = len(s), len(t)
48+
i = j = 0
49+
while i < m and j < n:
50+
if s[i] == t[j]:
51+
i += 1
52+
j += 1
53+
return i == m
4454
```
4555

4656
### **Java**
4757

4858
<!-- 这里可写当前语言的特殊实现逻辑 -->
4959

5060
```java
51-
61+
class Solution {
62+
public boolean isSubsequence(String s, String t) {
63+
int m = s.length(), n = t.length();
64+
int i = 0, j = 0;
65+
while (i < m && j < n) {
66+
if (s.charAt(i) == t.charAt(j)) {
67+
++i;
68+
}
69+
++j;
70+
}
71+
return i == m;
72+
}
73+
}
5274
```
5375

5476
### **...**

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,33 @@ If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you wan
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def isSubsequence(self, s: str, t: str) -> bool:
64+
m, n = len(s), len(t)
65+
i = j = 0
66+
while i < m and j < n:
67+
if s[i] == t[j]:
68+
i += 1
69+
j += 1
70+
return i == m
6371
```
6472

6573
### **Java**
6674

6775
```java
68-
76+
class Solution {
77+
public boolean isSubsequence(String s, String t) {
78+
int m = s.length(), n = t.length();
79+
int i = 0, j = 0;
80+
while (i < m && j < n) {
81+
if (s.charAt(i) == t.charAt(j)) {
82+
++i;
83+
}
84+
++j;
85+
}
86+
return i == m;
87+
}
88+
}
6989
```
7090

7191
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class Solution {
22
public boolean isSubsequence(String s, String t) {
3-
int index = -1;
4-
for (char c : s.toCharArray()) {
5-
index = t.indexOf(c, index + 1);
6-
if (index == -1) {
7-
return false;
3+
int m = s.length(), n = t.length();
4+
int i = 0, j = 0;
5+
while (i < m && j < n) {
6+
if (s.charAt(i) == t.charAt(j)) {
7+
++i;
88
}
9+
++j;
910
}
10-
return true;
11+
return i == m;
1112
}
12-
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def isSubsequence(self, s: str, t: str) -> bool:
3+
m, n = len(s), len(t)
4+
i = j = 0
5+
while i < m and j < n:
6+
if s[i] == t[j]:
7+
i += 1
8+
j += 1
9+
return i == m

0 commit comments

Comments
 (0)