JAVA实现strStr()的暴力匹配和KMP算法

package day07;
//给定一个 haystack 字符串和一个 needle 字符串,
// 在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。
//

public class strStr {
    public static void main(String[] args) {
        String haystack = "hello";
        String needle = "ll";//2
        //int firstIndex = strStrByViolence(haystack, needle);
        
        int firstIndex = strStrByKMP(haystack, needle);
        System.out.println(firstIndex);
    }

    //思路1:暴力匹配所有的可能
    public static int strStrByViolence(String haystack, String needle) {
        if (haystack == null || needle == null) {
            return 0;
        }
        //当needle为空时,应该返回0
        if ( needle.length() == 0) {
            return 0;
        }
        //当needle的长度大于haystack的长度时,肯定不存在
        if (needle.length() > haystack.length()) {
            return -1;
        }
        int i = 0;
        int j = 0;
        while (i < haystack.length() && j < needle.length()) {
            if (haystack.charAt(i) == needle.charAt(j)) {
                //判断是否匹配到最后一位
                if (j == needle.length() - 1) {
                    return i - j;
                } else {
                    i ++;
                    j ++;
                }

            } else {
                i = i - j + 1;//当前不匹配成功,回到上一次匹配开始的位置的下一个位置
                j = 0;//j 置零,重新匹配
            }
        }
        //找不到
        return -1;
    }

    //思路2:字符串匹配问题-->KMP算法
    public static int strStrByKMP(String haystack, String needle) {
        if (haystack == null || needle == null) {
            return 0;
        }
        //当needle为空时,应该返回0
        if ( needle.length() == 0) {
            return 0;
        }
        //当needle的长度大于haystack的长度时,肯定不存在
        if (needle.length() > haystack.length()) {
            return -1;
        }

        //kmp算法
        //获取next数组
        int[] next = getNext(needle);
        for(int i = 0, j = 0; i < haystack.length(); i ++) {
            while (j > 0 && haystack.charAt(i) != needle.charAt(j)) {
                j = next[j - 1];
            }
            if (haystack.charAt(i) == needle.charAt(j)) {
                j ++;
            }
            //判断是否结束
            if (j == needle.length()) {
                return i - j + 1;
            }
        }

        return -1;
    }
    //找到needle的前后最长公共子串的长度
    public static int[] getNext(String needle) {
        int[] next = new int[needle.length()];
        int j = 0;
        next[0] = 0;

        for(int i = 1; i < needle.length(); i ++) {
            while ( j > 0 && needle.charAt(i) != needle.charAt(j)) {
                j = next[j - 1];
            }
            if (needle.charAt(i) == needle.charAt(j)) {
                j ++;
            }
            next[i] = j;
        }

        return next;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值