首页 > 试题广场 >

子字符串频次

[编程题]子字符串频次
  • 热度指数:10361 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
请补全JavaScript代码,该函数接受两个参数分别为字符串、子字符串,要求返回子字符串在字符串中出现的频次。
方法一:暴力方法
        <script>
            const _searchStrIndexOf = (str, target) => {
                // 补全代码
                // 1.indexOf查找target在str中第一次出现的索引
                let index = str.indexOf(target);
                let num = 0;
                while(index > -1) {
                    // 继续向后查找str是否还有子串target
                    index = str.indexOf(target, index+1);
                    num++;
                }
                return num;
            }
        </script>

方法二:字符串分割
        <script>
            const _searchStrIndexOf = (str, target) => {
                // 补全代码
                var arr = str.split(target);
                return arr.length-1;
            }
        </script>


发表于 2022-03-20 22:58:12 回复(0)
 const _searchStrIndexOf = (str, target) => {
                // 补全代码
                let arr = str.split(target)
               return arr.length-1
            }

用字符串分割方式实现
发表于 2022-01-07 11:09:46 回复(2)
return str.split(target).length - 1;
发表于 2021-12-23 10:50:02 回复(6)
//将字符串通过子字符串拆分成数组,数组长度-1就是子字符串出现的次数
// 补全代码
let arr = str.split(target)
return arr.length-1

发表于 2021-12-08 09:17:03 回复(5)
        <script>
            const _searchStrIndexOf = (str, target) => {
                // 补全代码
                return str.split(target).length - 1
            }
        </script>


发表于 2023-02-14 02:06:52 回复(0)
<script>
        let str = "sssssst";
        let target = "ss";
        let i = 0;
        function _rank(str, target) {
            if (str.indexOf(target) != -1) {
                str = str.slice(str.indexOf(target) + 1)
                i++
                return _rank(str, target)
            }
            
        }
        _rank(str, target)
        console.log(i)
    </script>

发表于 2022-09-05 22:44:28 回复(0)
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
    </head>
    <body>

        <script>
             const _searchStrIndexOf = (str, target) => {

                let index = -1;
                let len = target.length;
                let sum = 0;
                do{
                    index = str.indexOf(target,index+len);
                    sum++;
                }while(index != -1)
            	    sum--;
                    return sum;
             }
        </script>
    </body>
</html>

发表于 2022-03-24 13:26:14 回复(0)
 const _searchStrIndexOf = (str, target) => {
     // 补全代码
     let index = str.indexOf(target);
     console.log(JSON.stringify({ str, target }));
     if (index == -1) {
         return 0;
     } else {
         return 1 + _searchStrIndexOf(str.slice(index + target.length), target);
     }
 }

编辑于 2024-03-26 04:31:24 回复(0)
 const _searchStrIndexOf = (str, target) => {
    // 补全代码
    let a = str.split(target)
    return a.length-1
}

发表于 2023-12-27 10:28:33 回复(0)
这里要搞清楚题意,是否一个已经找到的子字符串的部分可以被重复使用,比如子字符串aa,字符串aaa
发表于 2023-10-04 10:28:13 回复(0)
注意不要盲目用 const
发表于 2023-09-14 15:10:26 回复(0)
         const _searchStrIndexOf = (str, target) => {
      // 补全代码

      return str.split(target).length - 1

    }

发表于 2023-09-02 16:47:50 回复(0)
const _searchStrIndexOf = (str, target) => {
    let count = 0;
    while (1) {
        let index = str.lastIndexOf(target);
        if (index == -1) break;
        count++;
        str = str.slice(0, index);
    }
    return count;
};
发表于 2023-08-04 16:56:33 回复(0)
const _searchStrIndexOf = (str, target) => {
    const reg = new RegExp(target, 'g');
    const match = str.match(reg);
    if (match) {
        return match.length;
    }
    return 0;          
}

发表于 2023-07-25 10:06:37 回复(0)
const _searchStrIndexOf = (str, target) => {
// 补全代码
const regex = new RegExp(target, 'g');
const matches = str.match(regex);
const count = matches ? matches.length : 0;
return count
}
发表于 2023-07-10 16:49:16 回复(0)
const _searchStrIndexOf = (str, target) => {
    // 补全代码

    let num = 0
    for (let i = 0; i <= str.length - target.length; i++) {
        if (str.slice(i, i + target.length) === target) {
            num++
        }
    }
    return num
}
发表于 2023-06-17 15:49:59 回复(0)
<script>
const _searchStrIndexOf = (str, target) => {
    // 补全代码
    let reg = new RegExp(target,"g");
    return str.match(reg).length;
}
</script>

发表于 2023-05-14 23:36:41 回复(0)
  const _searchStrIndexOf = (str, target) => {
                // 补全代码
            var re = new RegExp(target,"g");
            var str2 =  str.match(re)
            return str2.length
               
            }
发表于 2023-03-16 16:14:39 回复(0)
<script>
            const _searchStrIndexOf = (str, target) => {
                // 补全代码
                //用子字符串作为断点,将父字符串分成一个数组,子字符串的个数即为数组长度减一
                let arr = str.split(target)
                return arr.length - 1
            }
        </script>
发表于 2023-02-13 15:34:31 回复(0)
 return str.split(target).length-1

发表于 2023-02-13 14:32:00 回复(0)