Skip to content

Commit 645d18e

Browse files
committed
feat: add solutions to lc problem: No.0392
No.0392.Is Subsequence
1 parent c50ddf5 commit 645d18e

File tree

4 files changed

+145
-32
lines changed

4 files changed

+145
-32
lines changed

Diff for: solution/0300-0399/0392.Is Subsequence/README.md

+57-16
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,6 @@ class Solution {
8787
}
8888
```
8989

90-
### **TypeScript**
91-
92-
```ts
93-
function isSubsequence(s: string, t: string): boolean {
94-
let m = s.length,
95-
n = t.length;
96-
let i = 0;
97-
for (let j = 0; j < n && i < m; ++j) {
98-
if (s.charAt(i) == t.charAt(j)) {
99-
++i;
100-
}
101-
}
102-
return i == m;
103-
}
104-
```
105-
10690
### **C++**
10791

10892
```cpp
@@ -135,6 +119,63 @@ func isSubsequence(s string, t string) bool {
135119
}
136120
```
137121

122+
### **TypeScript**
123+
124+
```ts
125+
function isSubsequence(s: string, t: string): boolean {
126+
let m = s.length,
127+
n = t.length;
128+
let i = 0;
129+
for (let j = 0; j < n && i < m; ++j) {
130+
if (s.charAt(i) == t.charAt(j)) {
131+
++i;
132+
}
133+
}
134+
return i == m;
135+
}
136+
```
137+
138+
### **Rust**
139+
140+
```rust
141+
impl Solution {
142+
pub fn is_subsequence(s: String, t: String) -> bool {
143+
let (s, t) = (s.as_bytes(), t.as_bytes());
144+
let n = t.len();
145+
let mut i = 0;
146+
for &c in s.iter() {
147+
while i < n && t[i] != c {
148+
i += 1;
149+
}
150+
if i == n {
151+
return false;
152+
}
153+
i += 1;
154+
}
155+
true
156+
}
157+
}
158+
```
159+
160+
### **C**
161+
162+
```c
163+
bool isSubsequence(char *s, char *t) {
164+
int n = strlen(s);
165+
int i = 0;
166+
for (int j = 0; j < n; j++) {
167+
while (t[i] && t[i] != s[j]) {
168+
i++;
169+
}
170+
if (!t[i]) {
171+
return 0;
172+
}
173+
i++;
174+
}
175+
return 1;
176+
}
177+
```
178+
138179
### **...**
139180
140181
```

Diff for: solution/0300-0399/0392.Is Subsequence/README_EN.md

+57-16
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,6 @@ class Solution {
6363
}
6464
```
6565

66-
### **TypeScript**
67-
68-
```ts
69-
function isSubsequence(s: string, t: string): boolean {
70-
let m = s.length,
71-
n = t.length;
72-
let i = 0;
73-
for (let j = 0; j < n && i < m; ++j) {
74-
if (s.charAt(i) == t.charAt(j)) {
75-
++i;
76-
}
77-
}
78-
return i == m;
79-
}
80-
```
81-
8266
### **C++**
8367

8468
```cpp
@@ -111,6 +95,63 @@ func isSubsequence(s string, t string) bool {
11195
}
11296
```
11397

98+
### **TypeScript**
99+
100+
```ts
101+
function isSubsequence(s: string, t: string): boolean {
102+
let m = s.length,
103+
n = t.length;
104+
let i = 0;
105+
for (let j = 0; j < n && i < m; ++j) {
106+
if (s.charAt(i) == t.charAt(j)) {
107+
++i;
108+
}
109+
}
110+
return i == m;
111+
}
112+
```
113+
114+
### **Rust**
115+
116+
```rust
117+
impl Solution {
118+
pub fn is_subsequence(s: String, t: String) -> bool {
119+
let (s, t) = (s.as_bytes(), t.as_bytes());
120+
let n = t.len();
121+
let mut i = 0;
122+
for &c in s.iter() {
123+
while i < n && t[i] != c {
124+
i += 1;
125+
}
126+
if i == n {
127+
return false;
128+
}
129+
i += 1;
130+
}
131+
true
132+
}
133+
}
134+
```
135+
136+
### **C**
137+
138+
```c
139+
bool isSubsequence(char *s, char *t) {
140+
int n = strlen(s);
141+
int i = 0;
142+
for (int j = 0; j < n; j++) {
143+
while (t[i] && t[i] != s[j]) {
144+
i++;
145+
}
146+
if (!t[i]) {
147+
return 0;
148+
}
149+
i++;
150+
}
151+
return 1;
152+
}
153+
```
154+
114155
### **...**
115156
116157
```

Diff for: solution/0300-0399/0392.Is Subsequence/Solution.c

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

Diff for: solution/0300-0399/0392.Is Subsequence/Solution.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn is_subsequence(s: String, t: String) -> bool {
3+
let (s, t) = (s.as_bytes(), t.as_bytes());
4+
let n = t.len();
5+
let mut i = 0;
6+
for &c in s.iter() {
7+
while i < n && t[i] != c {
8+
i += 1;
9+
}
10+
if i == n {
11+
return false;
12+
}
13+
i += 1;
14+
}
15+
true
16+
}
17+
}

0 commit comments

Comments
 (0)