Implement strStr()
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 129 ms)β
| Metric | Value |
|---|---|
| Runtime | 129 ms |
| Memory | N/A |
| Date | 2017-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β
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |