找出字符串中第一个匹配项的下标

1. 描述

给你两个字符串 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 。
 

来源:力扣(LeetCode)

2. 思路

这道题有两种解法:

1. 朴素解法:也就是每个字逐步匹配,遇到不能匹配的,从头开始匹配;这样时间复杂度为O(m*n) m是被匹配的字符串(haystack)长度;n是需匹配的字符串(needle)长度。

2. KMP法: 优先对需匹配的字符(needle)换建立一个prefix的相同前驱长度, 如下(列:abababc):

下标0123456
字符串abababc
相同前驱长度0012340

我们可以先列出所有的前驱,计算从左到右 和从右到左的相同字符串的最大长度

相同前驱长度所有前驱串说明
0a本身为0
0abab 没有相同的为0
1abaaba 一个相同
2abababab 两个相同
3ababaababa ababa 三个相同
4abababababab ababab 四个相同
0abababcabababc 没有相同为0
private int[] Build_PrefixTable(string pattern,int n)
{
        if (n == 0) return new int[0];
        int[] prefix = new int[n];
        int i = 1, j = 0;
        while(i < n)
        { 
            while (j > 0 && pattern[i] != pattern[j])
            {
                j = prefix[j - 1];
            }
            if (pattern[i] == pattern[j])
            {
                j++;
            }
            prefix[i] = j;
            i++;
        }
        return prefix;
}

然后对比被匹配(haystack)和需匹配(needle):

 public int StrStr(string haystack, string needle) 
{
        if(needle.Length>haystack.Length) return -1;        
        int n = needle.Length, m = haystack.Length;
        var prefix = Build_PrefixTable(needle,n);
        for(int i=0,j=0;i<m;i++)
        {
            while(j>0 && haystack[i]!= needle[j])
            {
                j=prefix[j-1];
            }
            if(haystack[i]==needle[j])
            {
                j++;
            }
            if(j==n)
            {
                return i-n +1;
            }
        }
        return -1;
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 可以的,以下是Java代码实现: ``` public int strStr(String haystack, String needle) { if (needle.isEmpty()) { return 0; } int hLen = haystack.length(); int nLen = needle.length(); if (hLen < nLen) { return -1; } for (int i = 0; i <= hLen - nLen; i++) { if (haystack.substring(i, i + nLen).equals(needle)) { return i; } } return -1; } ``` 这个函数接收两个字符串参数haystack和needle,返回needle在haystack第一次出现的位置,如果没有匹配,则返回-1。函数使用substring()函数来比较haystack的每个子字符串是否与needle相等,从而实现字符串匹配。 ### 回答2: 可以使用Java字符串方法来实现题目要求。以下是一种可能的解决方案: ```java public class Solution { public int strStr(String haystack, String needle) { if (needle.length() == 0) { return 0; } for (int i = 0; i <= haystack.length() - needle.length(); i++) { if (haystack.substring(i, i + needle.length()).equals(needle)) { return i; } } return -1; } } ``` 以上代码,我们首先判断 needle 是否为空字符串,如果是,则直接返回 0,因为空字符串一定是 haystack 的子串。然后,我们使用 for 循环遍历从 0 到 haystack.length() - needle.length() 的下标范围。在循环过程,我们使用 haystack.substring(i, i + needle.length()) 获取长度与 needle 相等的子串,并将其与 needle 进行比较。如果相等,则返回当前下标 i,表示找到了子串的第一个匹配。如果循环结束后仍未找到匹配,则说明 needle 不是 haystack 的子串,返回 -1。 以上就是根据题目要求用 Java 代码编写的解决方案。 ### 回答3: 可以使用Java代码来实现在haystack字符串找出needle字符串第一个匹配下标,代码如下所示: ```java public class FindIndex { public static int findIndex(String haystack, String needle) { if (haystack == null || needle == null || needle.length() > haystack.length()) { return -1; } int haystackLen = haystack.length(); int needleLen = needle.length(); for (int i = 0; i <= haystackLen - needleLen; i++) { int j; for (j = 0; j < needleLen; j++) { if (haystack.charAt(i + j) != needle.charAt(j)) { break; } } if (j == needleLen) { return i; } } return -1; } public static void main(String[] args) { String haystack = "hello world"; String needle = "world"; int index = findIndex(haystack, needle); System.out.println("在haystack字符串找到needle字符串第一个匹配下标为:" + index); } } ``` 以上代码通过双重循环遍历haystack字符串和needle字符串,逐个字符进行比较,如果找到相同的字符序列,则返回对应的下标位置。如果遍历完整个haystack字符串仍未找到匹配,则返回-1。在上述代码,给定的haystack字符串为"hello world",needle字符串为"world",其输出结果为: ``` 在haystack字符串找到needle字符串第一个匹配下标为:6 ``` 表示在haystack字符串下标为6处找到了needle字符串第一个匹配
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值