Skip to main content

KamaCoder-54, Replace Numbers

MikeLess than 1 minutestringeasystring

I Problem

Given a string s that contains lowercase alphabetic and numeric characters, write a function that leaves the alphabetic characters in the string unchanged and replaces each numeric character with a number. For example, for the input string "a1b2c3", the function should convert it to "anumberbnumbercnumber".

Example 1
Input: a1b2c3
Output: anumberbnumbercnumber

Constraints

  • 1 <= s.length < 10000

Related Topics

  • string

II Solution

Approach 1: Brute Force

pub fn replace_numbers(s: String) -> String {
    let number = "number";

    s.chars()
        .fold(String::with_capacity(s.len() * 2), |mut res, c| {
            if c.is_numeric() {
                res += number;
            } else {
                res.push(c);
            }
            res
        })
}