File tree 2 files changed +68
-0
lines changed 2 files changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * [0392] is-subsequence
3
+ */
4
+
5
+ pub struct Solution { }
6
+
7
+ // solution impl starts here
8
+
9
+ // 执行用时 : 4 ms , 在所有 Rust 提交中击败了 90.00% 的用户
10
+ // 内存消耗 : 3.3 MB , 在所有 Rust 提交中击败了 80.00% 的用户
11
+ // impl Solution {
12
+ // pub fn is_subsequence(s: String, t: String) -> bool {
13
+ // if s.len() == 0 {
14
+ // return true;
15
+ // }
16
+ // let mut i = 0;
17
+ // for c in t.chars() {
18
+ // if s[i..].chars().next().unwrap() == c {
19
+ // i += 1;
20
+ // }
21
+ // if i == s.len() {
22
+ // return true;
23
+ // }
24
+ // }
25
+ // false
26
+ // }
27
+ // }
28
+
29
+ // 执行用时 : 4 ms , 在所有 Rust 提交中击败了 90.00% 的用户
30
+ // 内存消耗 : 3.1 MB , 在所有 Rust 提交中击败了 100.00% 的用户
31
+ impl Solution {
32
+ pub fn is_subsequence ( s : String , t : String ) -> bool {
33
+ if s. len ( ) == 0 {
34
+ return true ;
35
+ }
36
+ let mut s_chars = s. chars ( ) ;
37
+ let mut s_char = s_chars. next ( ) ;
38
+ for c in t. chars ( ) {
39
+ if s_char. unwrap ( ) == c {
40
+ s_char = s_chars. next ( ) ;
41
+ }
42
+ if s_char. is_none ( ) {
43
+ return true ;
44
+ }
45
+ }
46
+ false
47
+ }
48
+ }
49
+
50
+ // solution impl ends here
51
+
52
+ // solution tests starts here
53
+
54
+ #[ cfg( test) ]
55
+ mod tests {
56
+ use super :: * ;
57
+
58
+ #[ test]
59
+ fn test_case0 ( ) {
60
+ assert_eq ! (
61
+ Solution :: is_subsequence( "abc" . to_owned( ) , "ahbgdc" . to_owned( ) ) ,
62
+ true
63
+ ) ;
64
+ }
65
+ }
66
+
67
+ // solution tests ends here
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ mod a0118_pascals_triangle;
20
20
mod a0119_pascals_triangle_ii;
21
21
mod a0145_binary_tree_postorder_traversal;
22
22
mod a0172_factorial_trailing_zeroes;
23
+ mod a0392_is_subsequence;
23
24
mod a0400_nth_digit;
24
25
mod a0404_sum_of_left_leaves;
25
26
mod a0405_convert_a_number_to_hexadecimal;
You can’t perform that action at this time.
0 commit comments