题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
//分离字符串
rl.on("line"function (line) {
  let input = line.split("");
  let char = [];
  //let words = line.match(/[a-z]+/ig).join('').split('')
  input.forEach((c=> {
    //   正则是否为字母
    let re = /[a-zA-Z]/;
    if (c.match(re)) {
      char.push(c);
    }
  });
  char.sort((a,b)=>{
      a = a.toLowerCase();
      b = b.toLowerCase();
      return a<b ? -10
  });
  let index = 0;
  let len = input.length;
//   替代字符串
  for (let i = 0i < leni++) {
    let re = /[a-zA-Z]/;
    if (input[i].match(re)) {
      input[i] = char[index];
      index++;
    }
  }
  console.log(input.join(""))
});

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务