File tree 4 files changed +145
-32
lines changed
solution/0300-0399/0392.Is Subsequence
4 files changed +145
-32
lines changed Original file line number Diff line number Diff line change @@ -87,22 +87,6 @@ class Solution {
87
87
}
88
88
```
89
89
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
-
106
90
### ** C++**
107
91
108
92
``` cpp
@@ -135,6 +119,63 @@ func isSubsequence(s string, t string) bool {
135
119
}
136
120
```
137
121
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
+
138
179
### **...**
139
180
140
181
```
Original file line number Diff line number Diff line change @@ -63,22 +63,6 @@ class Solution {
63
63
}
64
64
```
65
65
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
-
82
66
### ** C++**
83
67
84
68
``` cpp
@@ -111,6 +95,63 @@ func isSubsequence(s string, t string) bool {
111
95
}
112
96
```
113
97
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
+
114
155
### **...**
115
156
116
157
```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments