-
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 1750: Minimum Length of String After Deleting Similar Ends
- Loading branch information
Showing
3 changed files
with
89 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
64 changes: 64 additions & 0 deletions
64
src/problem_1750_minimum_length_of_string_after_deleting_similar_ends/iterative.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,64 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn minimum_length(s: String) -> i32 { | ||
let mut iter = s.as_bytes().iter(); | ||
let mut first = iter.next().copied().unwrap(); | ||
|
||
if let Some(mut last) = iter.next_back().copied() { | ||
loop { | ||
if first == last { | ||
// Consume left side that has the same value as `first`. | ||
|
||
loop { | ||
if let Some(&left) = iter.next() { | ||
if left != first { | ||
first = left; | ||
|
||
break; | ||
} | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
// Consume right side that has the same value as `last`. | ||
|
||
loop { | ||
if let Some(&right) = iter.next_back() { | ||
if right != last { | ||
last = right; | ||
|
||
break; | ||
} | ||
} else { | ||
return 1; | ||
} | ||
} | ||
} else { | ||
return iter.len() as i32 + 2; | ||
} | ||
} | ||
} else { | ||
1 | ||
} | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimum_length(s: String) -> i32 { | ||
Self::minimum_length(s) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/problem_1750_minimum_length_of_string_after_deleting_similar_ends/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,24 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn minimum_length(s: String) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
("ca", 2), | ||
("cabaabac", 0), | ||
("aabccabba", 3), | ||
("bbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbbbbbccbcbcbccbbabbb", 1), | ||
("c", 1), | ||
]; | ||
|
||
for (s, expected) in test_cases { | ||
assert_eq!(S::minimum_length(s.to_string()), expected); | ||
} | ||
} | ||
} |