-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1961: Check If String Is a Prefix of Array
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/problem_1961_check_if_string_is_a_prefix_of_array/dynamic_programming.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn is_prefix_string(s: String, words: Vec<String>) -> bool { | ||
let mut s = s.as_str(); | ||
|
||
for word in words { | ||
if let Some(next_s) = s.strip_prefix(word.as_str()) { | ||
s = next_s; | ||
} else { | ||
return s.is_empty(); | ||
} | ||
} | ||
|
||
s.is_empty() | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn is_prefix_string(s: String, words: Vec<String>) -> bool { | ||
Self::is_prefix_string(s, words) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/problem_1961_check_if_string_is_a_prefix_of_array/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
pub mod dynamic_programming; | ||
|
||
pub trait Solution { | ||
fn is_prefix_string(s: String, words: Vec<String>) -> bool; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(("iloveleetcode", &["i", "love", "leetcode", "apples"] as &[_]), true), | ||
(("iloveleetcode", &["apples", "i", "love", "leetcode"]), false), | ||
(("z", &["z"]), true), | ||
(("ccccccccc", &["c", "cc"]), false), | ||
]; | ||
|
||
for ((s, words), expected) in test_cases { | ||
assert_eq!( | ||
S::is_prefix_string(s.to_string(), words.iter().copied().map(str::to_string).collect()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |