mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 05:24:07 +00:00
day 3: 28-first-occurrence-of-substring
This commit is contained in:
parent
9da326288a
commit
688a352dbe
10
28-first-occurrence-of-substring/driver.cpp
Normal file
10
28-first-occurrence-of-substring/driver.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include "soln.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
string haystack = "Drink some water, reload your mags, and let's get back out there.";
|
||||
string needle = "water";
|
||||
Solution soln;
|
||||
cout << "Found correctly? " << soln.test(haystack, needle, 11) << endl;
|
||||
return 0;
|
||||
}
|
||||
18
28-first-occurrence-of-substring/soln.cpp
Normal file
18
28-first-occurrence-of-substring/soln.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int Solution::strStr(string haystack, string needle)
|
||||
{
|
||||
int index = haystack.find(needle);
|
||||
if (index != std::string::npos)
|
||||
{
|
||||
return index;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
bool Solution::test(string haystack, string needle, int answer) {
|
||||
return strStr(haystack, needle) == answer;
|
||||
}
|
||||
9
28-first-occurrence-of-substring/soln.hpp
Normal file
9
28-first-occurrence-of-substring/soln.hpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
int strStr(string haystack, string needle);
|
||||
bool test(string haystack, string needle, int answer);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user