From d27f6402261d8b79628317704916cea2c92bdd55 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 25 Apr 2024 10:02:32 +0800 Subject: [PATCH] 20240425 Finished --- src/problem/mod.rs | 3 ++- src/problem/p392_is_subsequence.rs | 40 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/problem/p392_is_subsequence.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 9772932..60b3922 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -111,4 +111,5 @@ mod p58_length_of_last_word; mod p151_reverse_words_in_a_string; mod p28_find_the_index_of_the_first_occurrence_in_a_string; mod p68_text_justification; -mod p125_valid_palindrome; \ No newline at end of file +mod p125_valid_palindrome; +mod p392_is_subsequence; \ No newline at end of file diff --git a/src/problem/p392_is_subsequence.rs b/src/problem/p392_is_subsequence.rs new file mode 100644 index 0000000..c936a35 --- /dev/null +++ b/src/problem/p392_is_subsequence.rs @@ -0,0 +1,40 @@ +/** + * [392] Is Subsequence + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn is_subsequence(s: String, t: String) -> bool { + let t : Vec = t.chars().collect(); + + let mut pos = 0; + for c in s.chars() { + while pos < t.len() && t[pos] != c { + pos += 1; + } + + if pos >= t.len() { + return false; + } + + pos += 1; + } + + true + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_392() { + assert!(Solution::is_subsequence("abc".to_owned(), "ahbgdc".to_owned())); + } +}