It checks for the case where needle length is 0, in which case the output is always 0 regardless of what haystack is. In cases where haystack is indeed 0 but needle is NOT empty, then we output -1.

class Solution {

public:

int strStr (string haystack, string needle) {

if (needle.length()==0) return 0;

if (haystack.length()==0 && needle.length()!=0) return -1;

```
for (int i = 0; ; ++i) {
for (int j = 0; ; ++j) {
if (needle[j] == 0) return i;
if (haystack[i + j] == 0) return -1;
if (haystack[i + j] != needle[j]) break;
}
}
}
```

};