-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_0000392_is_subsequence.rs
122 lines (111 loc) · 3.08 KB
/
_0000392_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 0000392. Is Subsequence
// https://leetcode.com/problems/is-subsequence/description/
// Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
//
// A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
//
// Example 1:
//
// Input: s = "abc", t = "ahbgdc"
// Output: true
//
// Example 2:
//
// Input: s = "axc", t = "ahbgdc"
// Output: false
//
// Constraints:
//
// 0 <= s.length <= 100
// 0 <= t.length <= 104
// s and t consist only of lowercase English letters.
//
// Follow up: Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?
// ======================
// Previous Approaches
// ======================
pub fn is_subsequence_previous_approach_1(s: String, t: String) -> bool {
let s_len = s.len();
if s_len == 0 {
return true;
}
let s_chars: Vec<char> = s.chars().collect();
let s_last_index = s_len - 1;
let mut j = 0;
for c in t.chars() {
if c == s_chars[j] {
j += 1;
}
if j > s_last_index {
return true;
}
}
false
}
// ---
pub fn is_subsequence(s: String, t: String) -> bool {
let s_len = s.len();
let t_len = t.len();
let s_char: Vec<char> = s.chars().collect();
let t_char: Vec<char> = t.chars().collect();
let mut s_idx = 0;
let mut t_idx = 0;
while s_idx < s_len && t_idx < t_len {
if s_char[s_idx] == t_char[t_idx] {
s_idx += 1;
}
t_idx += 1;
}
s_idx == s_len
}
// ---
pub fn testcase() {
let s = String::from("abc");
let t = String::from("ahbgdc");
let res = is_subsequence(s, t);
eprintln!("{} {:?}", module_path!(), res);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
let s = String::from("abc");
let t = String::from("ahbgdc");
let res = is_subsequence(s, t);
let expected = true;
assert_eq!(res, expected);
}
#[test]
fn test_2() {
let s = String::from("axc");
let t = String::from("ahbgdc");
let res = is_subsequence(s, t);
let expected = false;
assert_eq!(res, expected);
}
#[test]
fn test_3() {
let s = String::from("abc");
let t = String::from("abc");
let res = is_subsequence(s, t);
let expected = true;
assert_eq!(res, expected);
}
#[test]
fn test_4() {
let s = String::from("");
let t = String::from("abc");
let res = is_subsequence(s, t);
let expected = true;
assert_eq!(res, expected);
}
#[test]
fn test_5() {
let s = String::from("b");
let t = String::from("c");
let res = is_subsequence(s, t);
let expected = false;
assert_eq!(res, expected);
}
}