-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathImplementstrStr.cs
executable file
·43 lines (40 loc) · 2.11 KB
/
ImplementstrStr.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Source : https://leetcode.com/problems/implement-strstr/
// Author : codeyu
// Date : Thursday, October 13, 2016 11:07:52 PM
/**********************************************************************************
*
*
* Implement strStr().
*
*
* Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
public class Solution028
{
public static int StrStr(string haystack, string needle)
{
int i,j;
for(i=j=0;i<haystack.Length&&j<needle.Length;)
{
if(haystack[i] == needle[j])
{
i++;
j++;
}
else
{
i -= j -1;
j = 0;
}
}
return j != needle.Length ? -1 : i - j;
}
}
}