首页 > 代码库 > strstr

strstr

N(n*m)的时间复杂度

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

 

kmp:  N(m+n)

strstr