-
Notifications
You must be signed in to change notification settings - Fork 6
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
Showing
3 changed files
with
49 additions
and
1 deletion.
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
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 |
---|---|---|
|
@@ -98,3 +98,4 @@ mod max_beauty; | |
mod count_fair_pairs; | ||
mod square_numbers; | ||
mod minimized_maximum; | ||
mod remove_k_digits; |
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,46 @@ | ||
pub fn remove_kdigits(number: String, k: i32) -> String { | ||
let mut k = k as usize; | ||
let mut stack = String::with_capacity(number.len() - k); | ||
for digit in number.chars() { | ||
while k > 0 && !stack.is_empty() && digit < stack.chars().last().unwrap() { | ||
// Remove digits larger than the current one. | ||
stack.pop(); | ||
k -= 1; | ||
} | ||
if stack.is_empty() && digit == '0' { | ||
// Skip leading zeros. | ||
continue; | ||
} | ||
stack.push(digit); | ||
} | ||
|
||
// Remove any remaining right digits if not all digits were removed in the main loop. | ||
for _ in 0..k { | ||
stack.pop(); | ||
} | ||
|
||
match stack.is_empty() { | ||
true => "0".to_string(), | ||
false => stack, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::remove_kdigits; | ||
|
||
#[test] | ||
fn removes_k_digits_to_achieve_smallest_number_possible() { | ||
assert_eq!("1219".to_string(), remove_kdigits("1432219".to_string(), 3)) | ||
} | ||
|
||
#[test] | ||
fn digits_with_trailing_zeros() { | ||
assert_eq!("200".to_string(), remove_kdigits("10200".to_string(), 1)) | ||
} | ||
|
||
#[test] | ||
fn remove_all_digits() { | ||
assert_eq!("0".to_string(), remove_kdigits("10".to_string(), 2)) | ||
} | ||
} |