My C# solution for strStr()


  • 0
    D

    public class Solution {

    public int StrStr(string haystack, string needle) {
        for (int i = 0; i < haystack.Length - needle.Length + 1 ; i++)
            if (compare(haystack, i, needle)) return i;
        return -1;
    }
    
    bool compare(string haystack, int start, string needle){
       for (int  i = 0; i < needle.Length; i++)
           if (haystack[start + i] != needle[i]) return false;
       return true;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.