-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8cbbf55
commit c23b9e1
Showing
6 changed files
with
80 additions
and
19 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/problems/_01_arrays_and_strings/_06_string_compression.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 @@ | ||
use std::borrow::Cow; | ||
|
||
pub trait StringCompression { | ||
/// Implement a method to perform basic string compression using the counts | ||
/// of repeated characters. For example, the string aabcccccaaa would become | ||
/// a2b1c5a3. If the compressed string would not become smaler than the | ||
/// original string, your method should return the original string. You can | ||
/// assume the string has only uppercase and lowercase letters. | ||
fn string_compression(string: &str) -> Cow<str>; | ||
} | ||
|
||
struct Solution; | ||
|
||
impl StringCompression for Solution { | ||
fn string_compression(string: &str) -> Cow<str> { | ||
// Replace with your solution | ||
use crate::solutions::_01_arrays_and_strings::_06_string_compression as solutions; | ||
solutions::Solution::string_compression(string) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn compresses() { | ||
assert_eq!(Solution::string_compression("aabcccccaaa"), "a2b1c5a3"); | ||
} | ||
|
||
#[test] | ||
fn returns_original_string() { | ||
assert_eq!(Solution::string_compression("abca"), "abca"); | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
src/solutions/_01_arrays_and_strings/_06_string_compression.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,41 @@ | ||
use crate::problems::_01_arrays_and_strings::_06_string_compression::StringCompression; | ||
use std::borrow::Cow; | ||
|
||
pub struct Solution; | ||
|
||
impl StringCompression for Solution { | ||
fn string_compression(string: &str) -> Cow<str> { | ||
let mut chars = string.chars(); | ||
let mut previous = match chars.next() { | ||
Some(c) => c, | ||
None => return Cow::Borrowed(string), | ||
}; | ||
loop { | ||
match chars.next() { | ||
Some(c) => { | ||
if c == previous { | ||
break; | ||
} | ||
previous = c; | ||
} | ||
None => return Cow::Borrowed(string), | ||
} | ||
} | ||
|
||
let mut chars = string.chars(); | ||
let mut previous = chars.next().unwrap(); | ||
let mut count = 1u32; | ||
let mut out = String::new(); | ||
for c in chars { | ||
if c != previous { | ||
out.extend(format!("{previous}{count}").chars()); | ||
previous = c; | ||
count = 1; | ||
} else { | ||
count += 1; | ||
} | ||
} | ||
out.extend(format!("{previous}{count}").chars()); | ||
Cow::Owned(out) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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