Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8585955

Browse files
author
guangsheng.li01
committed
Solved 0392
1 parent 6c280e4 commit 8585955

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/a0392_is_subsequence.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod a0118_pascals_triangle;
2020
mod a0119_pascals_triangle_ii;
2121
mod a0145_binary_tree_postorder_traversal;
2222
mod a0172_factorial_trailing_zeroes;
23+
mod a0392_is_subsequence;
2324
mod a0400_nth_digit;
2425
mod a0404_sum_of_left_leaves;
2526
mod a0405_convert_a_number_to_hexadecimal;

0 commit comments

Comments
 (0)