Skip to content

Commit

Permalink
feat: 3174
Browse files Browse the repository at this point in the history
  • Loading branch information
ben1009 committed Sep 11, 2024
1 parent 1f4f0bf commit 2a099ea
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/problem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ pub mod p2938_separate_black_and_white_balls;
pub mod p2956_find_common_elements_between_two_arrays;
pub mod p2974_minimum_number_game;
pub mod p3033_modify_the_matrix;
pub mod p3174_clear_digits;
74 changes: 74 additions & 0 deletions src/problem/p3174_clear_digits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// You are given a string s.

// Your task is to remove all digits by doing this operation repeatedly:

// Delete the first digit and the closest non-digit character to its left.
// Return the resulting string after removing all digits.

// Example 1:

// Input: s = "abc"

// Output: "abc"

// Explanation:

// There is no digit in the string.

// Example 2:

// Input: s = "cb34"

// Output: ""

// Explanation:

// First, we apply the operation on s[2], and s becomes "c4".

// Then we apply the operation on s[1], and s becomes "".

// Constraints:

// 1 <= s.length <= 100
// s consists only of lowercase English letters and digits.
// The input is generated such that it is possible to delete all digits.

// problem: https://leetcode.com/problems/clear-digits/

// submission codes start here
pub struct Solution {}

impl Solution {
pub fn clear_digits(s: String) -> String {
if s.len() == 1 {
return s;
}

let mut ret = Vec::new();
for c in s.chars() {
if c.is_alphabetic() {
ret.push(c);
} else if ret.last().unwrap_or(&'a').is_alphabetic() {
ret.pop();
} else {
ret.push(c);
}

Check warning on line 55 in src/problem/p3174_clear_digits.rs

View check run for this annotation

Codecov / codecov/patch

src/problem/p3174_clear_digits.rs#L54-L55

Added lines #L54 - L55 were not covered by tests
}

ret.iter().collect()
}
}

// submission codes end

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

#[test]
fn test_3174() {
assert_eq!(Solution::clear_digits("abc".to_string()), "abc".to_string());
assert_eq!(Solution::clear_digits("cb34".to_string()), "".to_string());
assert_eq!(Solution::clear_digits("1".to_string()), "1".to_string());
}
}

0 comments on commit 2a099ea

Please sign in to comment.