- Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha0392_is_subsequence.rs
67 lines (58 loc) · 1.54 KB
/
a0392_is_subsequence.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* [0392] is-subsequence
*/
pubstructSolution{}
// solution impl starts here
// 执行用时 : 4 ms , 在所有 Rust 提交中击败了 90.00% 的用户
// 内存消耗 : 3.3 MB , 在所有 Rust 提交中击败了 80.00% 的用户
// impl Solution {
// pub fn is_subsequence(s: String, t: String) -> bool {
// if s.len() == 0 {
// return true;
// }
// let mut i = 0;
// for c in t.chars() {
// if s[i..].chars().next().unwrap() == c {
// i += 1;
// }
// if i == s.len() {
// return true;
// }
// }
// false
// }
// }
// 执行用时 : 4 ms , 在所有 Rust 提交中击败了 90.00% 的用户
// 内存消耗 : 3.1 MB , 在所有 Rust 提交中击败了 100.00% 的用户
implSolution{
pubfnis_subsequence(s:String,t:String) -> bool{
if s.len() == 0{
returntrue;
}
letmut s_chars = s.chars();
letmut s_char = s_chars.next();
for c in t.chars(){
if s_char.unwrap() == c {
s_char = s_chars.next();
}
if s_char.is_none(){
returntrue;
}
}
false
}
}
// solution impl ends here
// solution tests starts here
#[cfg(test)]
mod tests {
usesuper::*;
#[test]
fntest_case0(){
assert_eq!(
Solution::is_subsequence("abc".to_owned(),"ahbgdc".to_owned()),
true
);
}
}
// solution tests ends here