题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void async function () { // Write your code here let token = [];//while函数中的tokens是let声明,在函数外拿不到,在函数外声明一个变量拿到tokens中的数据。 while(line = await readline()){ // let tokens = line.split(' '); token.push(line); } token[0] = token[0].toUpperCase();//字符串变成大写。.toLowerCase()//字符串变成小写 token[1] = token[1].toUpperCase(); // console.log(token) let a = 0; while(token[0].indexOf(token[1]) !== -1){ a++; token[0] = token[0].substring(token[0].indexOf(token[1])+1,token[0].length) //取token[0]中index为token[0].indexOf(token[1])+1到token[0].length的一段赋值给token[0]。 } console.log(a); }()
我的方法
void async function () { // Write your code here let result = [] let count = 0 while(line = await readline()){ result.push(line) if( result.length == 2){ result[0].toUpperCase().split("").forEach(el =>{ //字符串中的每个字符都单独分割。对分割后得到的数组中的每一项el都做forEach函数 el == line.toUpperCase()?count++:'' //三目运算符 }) console.log(count) } } }()
牛客469089949号——的方法
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; const arr = []; void (async function () { // Write your code here while ((line = await readline())) { let tokens = line.toString();//转化成字符 arr.push(tokens.toLowerCase()); } let count = 0; for (let k = 0; k < arr[0].length; k++) { if (arr[0][k] === arr[1]) {//字符串就是一个字符组成的数组,arr[0][k]的每一项都是一个字符 count++; } } console.log(count); })();
ycljy——的方法
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void async function () { // Write your code here let arr = [] while(line = await readline()){ arr.push(line.toLowerCase()) } let first = arr[0] let second = arr[1] let firstArr = first.split('') let newArr = firstArr.filter(item => item === second) // let newArr = firstArr.filter(function(item){ // if(item === second){ // return item; // } // }) console.log(newArr.length) }()
牛客944083017号——的方法
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void async function () { // Write your code here let arr = [] while(line = await readline()){ arr.push(line.toLowerCase()) } let first = arr[0] let second = arr[1] let newArr = first.split(second)//用second做分割,太聪明啦! console.log(newArr.length - 1) }()
牛客944083017号——的方法2
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const arr = [];//创建一个数组,用来放输入的字符串和需要搜索的字符 rl.on('line',function (line){ arr.push(line); if(arr.length === 2){ const input = arr[0]; //这是输入的字符串 const target = arr[1];//这是需要搜索的字符 var res = input.match(new RegExp(target,"gim")); //res用来放所有在传入的字符串input中找到的字符target if(res === null) console.log(0); else console.log(res.length);//如果res为null就是没找到,否则就返回找到的个数res.length } });
牛客848877901号——的方法
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let arr = []; rl.on("line", function (line) { const tokens = line.split(" "); arr.push(line); if(arr.length==2){ let matchStrArr = arr[0].match(new RegExp(arr[1],'gi')); console.log(matchStrArr ? matchStrArr.length : 0);//三目运算符 } });
烤只因——的方法
2023年6月3日
练练练练练 文章被收录于专栏
练练练练练