使用 JavaScript 统计并返回出现在 str2 中的 str1 字数
问题
我们需要编写一个 JavaScript 函数,它把两个字符串作为第一个和第二个参数接收,分别是 str1 和 str2。
我们的函数应该统计并返回出现在 str2 中的 str1 字符数,如果有重复出现,我们必须单独统计。
例如,如果输入函数的是
输入
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP