-
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 1957: Delete Characters to Make Fancy String
- Loading branch information
Showing
3 changed files
with
71 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
52 changes: 52 additions & 0 deletions
52
src/problem_1957_delete_characters_to_make_fancy_string/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,52 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn make_fancy_string(s: String) -> String { | ||
let mut s = s.into_bytes(); | ||
let slice = s.as_mut_slice(); | ||
let mut writer = 0; | ||
let mut i = 0; | ||
let mut prev = 0; | ||
let mut count = 0_u8; | ||
|
||
while let Some(&c) = slice.get(i) { | ||
i += 1; | ||
|
||
if c == prev { | ||
if count < 2 { | ||
count += 1; | ||
} else { | ||
continue; | ||
} | ||
} else { | ||
prev = c; | ||
count = 1; | ||
} | ||
|
||
slice[writer] = c; | ||
writer += 1; | ||
} | ||
|
||
s.truncate(writer); | ||
|
||
String::from_utf8(s).unwrap() | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn make_fancy_string(s: String) -> String { | ||
Self::make_fancy_string(s) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/problem_1957_delete_characters_to_make_fancy_string/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,18 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn make_fancy_string(s: String) -> String; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [("leeetcode", "leetcode"), ("aaabaaaa", "aabaa"), ("aab", "aab")]; | ||
|
||
for (s, expected) in test_cases { | ||
assert_eq!(S::make_fancy_string(s.to_string()), expected); | ||
} | ||
} | ||
} |