算法(TS): 实现 strStr()

给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。

示例 1:

输入:haystack = "sadbutsad", needle = "sad"

输出:0

解释:"sad" 在下标 0 和 6 处匹配。

第一个匹配项的下标是 0 ,所以返回 0 。

示例 2:

输入:haystack = "leetcode", needle = "leeto"

输出:-1

解释:"leeto" 没有在 "leetcode" 中出现,所以返回 -1 。

提示:

  • 1 <= haystack.length, needle.length <= 104
  • haystack 和 needle 仅由小写英文字符组成

解答

遍历 haystack,当 haystacki === needle0时,再比较 haystacki + 1 与 needle1是否相等,haystacki + 2 与 needle2 是否相等,一直到 haystacki + needle.length - 1 与 needleneedle.length - 1 是否相等。这个过程一共有两层遍历,当 haystacki === needle0 满足时,才开始第二层遍历

ini 复制代码
function strStr(haystack: string, needle: string): number {
    let index = -1
    let i = 0
    while(i < haystack.length) {
        if (haystack[i] === needle[0]) {
            let step = 1
            let isInclude = true
            while(step < needle.length) {
                if (haystack[i + step] !== needle[step]) {
                    isInclude = false
                    break
                }
                step++
            }

            if (isInclude) {
                index = i
                break
            }
        }

        i++
    }

    return index
};

空间复杂度O(1),时间负责度O(m * n)

needle 是 haystack 的一部分,意味着 haystack 的长度不小于 needle 的长度,因此对上面的解答进行优化

ini 复制代码
function strStr(haystack: string, needle: string): number {
    let index = -1
    let i = 0
    while(i < haystack.length) {
        if (haystack.length - i < needle.length) {
            break
        }
        if (haystack[i] === needle[0]) {
            let step = 1
            let isInclude = true
            while(step < needle.length) {
                if (haystack[i + step] !== needle[step]) {
                    isInclude = false
                    break
                }
                step++
            }

            if (isInclude) {
                index = i
                break
            }
        }

        i++
    }

    return index
};

在上述解法中,每一次遇到不匹配的位置都是将 needle 往后移动一步,利用KMP算法计算出部分匹配表,动态计算需要移动的步数

ini 复制代码
function strStr(haystack: string, needle: string): number {
    const n = haystack.length, m = needle.length;
    let index = -1
    let i = 0
    // 部分匹配表
    const pi = new Array(m).fill(0);
    for (let i = 1, j = 0; i < m; i++) {
        while (j > 0 && needle[i] !== needle[j]) {
            j = pi[j - 1];
        }
        if (needle[i] == needle[j]) {
            j++;
        }
        pi[i] = j;
    }

    while(i < n) {
        let j = 1
        if (n - i < m) {
            break
        }
        if (haystack[i] === needle[0]) {
            let step = 1
            let isInclude = true
            while(step < m) {
                if (haystack[i + step] !== needle[step]) {
                    isInclude = false
                    // needle 移动步数 = 已匹配的字符数 - 对应的部分匹配值
                    j = step - pi[step-1]
                    break
                }
                step++
            }

            if (isInclude) {
                index = i
                break
            }
        }

        i += j
    }

    return index
};

时间复杂度O(m+n),空间复杂度O(m)

相关推荐
可编程芯片开发7 分钟前
基于PI控制算法的pwm直流电机控制系统Simulink建模与仿真
算法
怕浪猫26 分钟前
2840亿参数只卖白菜价:DeepSeek V4 Flash 正式版上线,Agent 能力暴涨6倍
人工智能·算法
让学习成为一种生活方式32 分钟前
苄基异喹啉生物碱糖基转移酶UGT74AN1晶体--Journal of Agricultural and Food Chemistry
人工智能·算法
alphaTao35 分钟前
LeetCode 每日一题 2026/7/27-2026/8/2
python·算法·leetcode
2501_926978331 小时前
提示工程的实战报告(二):模型的失败模式与边界行为
人工智能·深度学习·算法
小小晓.1 小时前
C++记:函数
开发语言·c++·算法
casual~1 小时前
模逆元计算方法详解:扩展欧几里得算法与费马小定理
学习·算法·逆元
脚踏实地皮皮晨2 小时前
002002002_DepandencyObject类2
开发语言·windows·算法·c#·visual studio
贩卖黄昏的熊3 小时前
NestJS简明教程——安全
开发语言·javascript·安全·typescript·nest.js
theoweb33 小时前
Typescript关于sort需要注意的地方
typescript