Skip to main content

Implement strStr()

LeetCode Link

Problem Description​

Visit LeetCode for the full problem description.


Solutions​

Solution 1: C# (Best: 129 ms)​

MetricValue
Runtime129 ms
MemoryN/A
Date2017-09-03
Solution
public class Solution {
public int StrStr(string haystack, string needle) {
if(needle.Length > haystack.Length) return -1;
for (int i = 0; ; i++)
{
for (int j = 0; ; j++)
{
if(j==needle.Length) return i;
if(i+j == haystack.Length) return -1;
if(needle[j] != haystack[i+j]) break;
}
}
}
}

Complexity Analysis​

ApproachTimeSpace
SolutionTo be analyzedTo be analyzed