Leetcode 28. Implement strStr() [easy][java] [KMP Solution]

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example
在这里插入图片描述
Ignored Cases
在这里插入图片描述

==Solution 1: iteration ==
Time Complexity: O((n-m)m) where n is haystack’s length and m is needle’s length. Space Complexity: O(1)

class Solution {
    public int strStr(String haystack, String needle) {
        if(haystack == null  || haystack.length() < needle.length())
            return -1;
        
        if(needle == null || needle.isEmpty())
            return 0;
        
        int hLen = haystack.length();
        int nLen = needle.length();
        for(int i = 0; i<= hLen-nLen; i++) {
            int j = 0;
            for(; j < nLen; j++) {
                if(haystack.charAt(i+j) != needle.charAt(j))
                    break;
            }
            if(j == nLen)
                return i;
        }
        return -1;
    }
}

Solution 2: KMP
consideration

  1. we use a next array to store the longest length of the prefix common to the postfix right before the current index. next[0] = -1 since there is no substring before index 0.
  2. For current index j, if p[j] != p [k+1], we let k = next[k]. Afterwards, we check if p[j] == p[k+1], we increase k by 1. Finally, set p[j] = k.
  3. For two string checks, we use i to iterate haystack and j to iterate needle.
  4. if(haystack[i] == needle[j]), we increase i and j both by 1.
  5. if(j == 0) which means we have reached the beginning of needle and haystack[i] != needle[j], then we increase i by 1.
  6. if(j > 0) and haystack[i] != needle[j], we let j be next[j-1]+1.
  7. if(j == needle.length), then the substring has been found, we return i-j.
class Solution {
    public int strStr(String haystack, String needle) {
        if(haystack == null  || haystack.length() < needle.length())
            return -1;
        
        if(needle == null || needle.isEmpty())
            return 0;
        
        int hLen = haystack.length();
        int nLen = needle.length();
        int[] next = next(needle);
        
        for(int i = 0, j=0; i< hLen; ) {
            if(haystack.charAt(i) == needle.charAt(j)) {
                i++;
                j++;
            } else if(j == 0 && haystack.charAt(i) != needle.charAt(j)){
                i++;
            } else if(j > 0 && haystack.charAt(i) != needle.charAt(j)) {
                j = next[j-1]+1;
            }

            if(j == nLen)
                return i-j;
        }
        return -1;
    }
    
     private int[] next(String p) {
         int[] next = new int[p.length()];
         next[0] = -1;
         int k = -1;
         int j = 1;
         while(j < p.length()) {
             while(k > -1 && p.charAt(j) != p.charAt(k+1)) {
                 k = next[k];
             }
             if(p.charAt(j) == p.charAt(k+1))
                 k = k+1;
             next[j++] = k;
         }
         
         return next;
     }
}

Reference

  1. https://www.cnblogs.com/zhangtianq/p/5839909.html
    2. https://blog.csdn.net/starstar1992/article/details/54913261
  2. http://www.matrix67.com/blog/archives/115
  3. https://www.jianshu.com/p/8146e8598490
  4. https://www.cnblogs.com/yjiyjige/p/3263858.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值