使用 JavaScript 统计并返回 str1 中出现在 str2 中的字符数
问题
我们需要编写一个JavaScript函数,它接受两个字符串str1和str2分别作为第一个和第二个参数。
我们的函数应该统计并返回str1中出现在str2中的字符数,如果出现重复出现,我们需要单独统计。
例如,如果函数的输入是
输入
const str1 = 'Kk'; const str2 = 'klKKkKsl';
输出
const output = 5;
示例
以下是代码-
const str1 = 'Kk';
const str2 = 'klKKkKsl';
var countAppearances = (str1 = '', str2 = '') => {
const map = {}
for(let c of str1) {
map[c] = true
}
let count = 0
for(let c of str2) {
if(map[c]) {
count+=1
}
}
return count
};
console.log(countAppearances(str1, str2));输出结果5
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练