28. Implement strStr()

Input: haystack = "hello", needle = "ll"
Output: 2
Input: haystack = "aaaaa", needle = "bba"
Output: -1
Input: haystack = "", needle = ""
Output: 0
class Solution {
public:
int strStr(string haystack, string needle) {

int pos = haystack.find(needle);

if(pos != string::npos)
return pos;

return -1;
}
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store