Skip to content

Commit a4b3740

Browse files
authored
Create Leetcode_28.cpp
Use only the solution block to submit code on Leetcode
1 parent ae04a46 commit a4b3740

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Leetcode_28.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream> // Include the input-output stream library
2+
#include <string> // Include the string library
3+
4+
using namespace std; // Use the standard namespace to avoid prefixing std::
5+
6+
// Define a class named Solution
7+
class Solution {
8+
public:
9+
// Function to find the first occurrence of "needle" in "haystack"
10+
int strStr(string haystack, string needle) {
11+
return haystack.find(needle); // Using the find() function of the string class
12+
}
13+
};
14+
15+
int main()
16+
{
17+
string haystack = "hello"; // Define the main string where we search
18+
string needle = "ll"; // Define the substring to be found
19+
20+
Solution a; // Create an instance of the Solution class
21+
int result = a.strStr(haystack, needle); // Call strStr() and store the result
22+
23+
cout << result << endl; // Print the result (index of first occurrence or -1 if not found)
24+
25+
return 0; // Indicate successful program termination
26+
}

0 commit comments

Comments
 (0)