-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path28.cpp
More file actions
25 lines (25 loc) · 665 Bytes
/
28.cpp
File metadata and controls
25 lines (25 loc) · 665 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Author: btjanaka (Bryon Tjanaka)
// Problem: (Leetcode) 28
// Title: Implement strStr()
// Link: https://leetcode.com/problems/implement-strstr
// Idea:
// Difficulty: easy
// Tags: implementation, strings
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle.empty()) return 0;
if (haystack.size() < needle.size()) return -1;
for (int i = 0; i <= haystack.size() - needle.size(); ++i) {
bool match = true;
for (int j = 0; j < needle.size(); ++j) {
if (haystack[i + j] != needle[j]) {
match = false;
break;
}
}
if (match) return i;
}
return -1;
}
};