Skip to content

Commit

Permalink
Added make the string great solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
jusexton committed Apr 5, 2024
1 parent 8f03a0b commit 896c0aa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ other languages.
- Unique Number of Occurrences (https://leetcode.com/problems/unique-number-of-occurrences)
- Minimum Common Value (https://leetcode.com/problems/minimum-common-value)
- Sort by Number of Bits (https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits)
- Make the String Great (https://leetcode.com/problems/make-the-string-great/)

#### Medium

Expand Down
26 changes: 26 additions & 0 deletions src/leetcode/make_string_great.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fn make_good(s: String) -> String {
let mut result = String::with_capacity(s.len());
for char in s.chars() {
if !result.is_empty() && (char as u8).abs_diff(result.chars().last().unwrap() as u8) == 32 {
result.pop();
} else {
result.push(char);
}
}
result
}

#[cfg(test)]
mod tests {
use test_case::test_case;

use crate::leetcode::make_string_great::make_good;

#[test_case("leEeetcode", "leetcode")]
#[test_case("abBAcC", "")]
#[test_case("s", "s")]
fn should_produce_length_of_largest_substring_between_duplicates(s: &str, expected: &str) {
let actual = make_good(s.to_string());
assert_eq!(actual, expected.to_string());
}
}
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod largest_number;
mod largest_odd_number;
mod lonely_numbers;
mod longest_subtsring;
mod make_string_great;
mod max_substr_between_duplicates;
mod merge_lists;
mod minimum_common_value;
Expand Down

0 comments on commit 896c0aa

Please sign in to comment.