Files
LeetCode/Easy/implement-strstr/solution.cpp
Arkaprabha Chakraborty cef4c4dcc4 Initial commit
2022-07-26 16:15:18 +05:30

26 lines
685 B
C++

#include <string>
class Solution {
public:
int strStr(std::string haystack, std::string needle) {
for (int i = 0; i < haystack.size(); ++i) {
if (haystack.at(i) == needle.at(0)) {
bool found = false;
for (int j = 0; i + j < haystack.size() && j < needle.size(); ++j) {
if (haystack.at(i + j) != needle.at(j)) {
break;
} else if (j == needle.size() - 1) {
found = true;
}
}
if (found) {
return i;
}
}
}
return -1;
}
};