Leetcode Implement strStr (C,c++,java,python)

Source: Internet
Author: User
Tags first string

Problem:

Implement strStr ().

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

Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char * or String , please click on the Reload button to reset your Code definition.

Solution:O(nm) Runtime,O(1) Space–brute Force:

You could demonstrate to your interviewer the This problem can be solved using known efficient algorithms such as rabin-k ARP algorithm, KMP algorithm, and the Boyer-moore algorithm. Since These algorithms is usually studied in a advanced algorithms class, it's sufficient to solve it using the most DI Rect method in a Interview–the brute force method.

The brute force method was straightforward to implement. We Scan the needle with the haystack from their first position and start matching all subsequent letters one by one. If One of the letters does not match, we start over again with the next position in the haystack.

The key is to implement the solution cleanly without dealing with each edge case separately.

Classic KMP Application, violence or not to mention, refer to here: From beginning to end thoroughly understand KMP


Title: To two strings, the second string in the first string to appear in the smallest position, if not appear, then output-1

Java source Code (323MS):
public class Solution {public    int strStr (String haystack, string needle) {        char[] Chs1=haystack.tochararray (); C2/>char[] Chs2=needle.tochararray ();        int len1=chs1.length,len2=chs2.length;        Int[] Next=new int[len2+1];        GetNext (CHS2,NEXT,LEN2);        int i=0,j=0;        while (I<len1 && j<len2) {            if (J==-1 | | chs1[i]==chs2[j]) {                i++;j++;            } else{                j=next[j];            }        }        if (j<len2) return-1;        return i-len2;    }    private void GetNext (char[] chs,int[] Next,int len) {        int i=0,j=-1;        Next[0]=-1;        while (I<len) {            if (J==-1 | | chs[i]==chs[j]) {                i++;j++;                next[i]=j;            } else{                j=next[j];}}}    


C Language Source code (2MS):
void GetNext (char *needle,int* next) {    int i=0,j=-1;    Next[0]=-1;    while (Needle[i]) {        if (J==-1 | | needle[j]==needle[i]) {            j + +;            i++;            next[i]=j;        } else{            j=next[j];}}    } int strStr (char* haystack, char* needle) {    int length=strlen (needle), i=0,j=0;    int *next= (int*) malloc (sizeof (int) * (length+1));    GetNext (needle,next);    while (J==-1 | | (Haystack[i] && needle[j])) {        if (J==-1 | | haystack[i]==needle[j]) {            j++;i++;        } else{            j=next[j];        }    }    if (Needle[j]) return-1;    else return i-length;}

C + + source code (8MS):
Class Solution {public:    int strStr (String haystack, string needle) {        int len1=haystack.size (), len2=needle.size ();        int* next= (int*) malloc (sizeof (int) * (len2+1));        GetNext (NEEDLE,NEXT,LEN2);        int i=0,j=0;        while (I<len1 && j<len2) {            if (J==-1 | | haystack[i]==needle[j]) {                i++;j++;            } else{                j=next[j];            }        }        if (j<len2) return-1;        else return i-len2;    } Private:    void GetNext (string needle,int* next,int len1) {        int i=0,j=-1;        Next[0]=-1;        while (I<LEN1) {            if (J==-1 | | needle[i]==needle[j]) {                j++;i++;                next[i]=j;            } else{                j=next[j];}}}    ;

Python source code (74MS):
Class solution:    # @param {string} haystack    # @param {string} needle    # @return {integer}    def strStr (self, Haystack, needle):        Len1=len (haystack); Len2=len (needle)        next=[-1 for I in Range (len2+1)]        Self.getnext ( NEEDLE,NEXT,LEN2)        i=0;j=0 while        i<len1 and j<len2:            if J==-1 or haystack[i]==needle[j]:                i+=1;j +=1            Else:j=next[j]        if j<len2:return-1        else:return i-len2    def getNext (self,needle,next,len2 ):        i=0;j=-1 while        i<len2:            if J==-1 or needle[i]==needle[j]:                i+=1                j+=1                next[i]=j            Else:j=next[j]


Leetcode Implement strStr (C,c++,java,python)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.