diff --git a/C++/28_first_occurrence_in_a_string.cpp b/C++/28_first_occurrence_in_a_string.cpp new file mode 100644 index 0000000..d507c36 --- /dev/null +++ b/C++/28_first_occurrence_in_a_string.cpp @@ -0,0 +1,26 @@ +/** + * easy difficulty + * runtime: 0ms + * beats 100% + * + * memory: 8.85mb + * beats 99.68% + */ + +class Solution { +public: + /** + * @brief a function will return the index of the first occurence of + * a string in the main string + * @param haystack is the main string that the search will be applied to + * @param needle is the string that the function will search for + * @return the index of the first occurence of 'needle' in 'haystack' if + * found ; std::string::npos (or -1) otherwise + */ + constexpr inline int strStr(const string& haystack, const string& needle) noexcept { + // using built in functions to find the index of the + // first occurence of the 'needle' in the 'haystack' + // and if 'needle' isn't present, it will return std::string::npos (-1) + return haystack.find(needle); + } +}; diff --git a/README.md b/README.md index f509980..2bbe043 100644 --- a/README.md +++ b/README.md @@ -294,9 +294,10 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | --- | --------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | ---------------------- | ------------------ | ---------- | ----------- | ---------------- | | 005 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [Python](./Python/5_LongestPalindromicSubstring.py)
[JavaScript](./JavaScript/5.Longest-Palindromic-Substring.js) | _O(N^2)_
_O(N^2)_ | _O(N)_
_O(1)_ | Medium | | Expand the Wings | | 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Java](./Java/median-of-two-sorted-arrays.java) | _O(log(min(m,n)))_ | _O(1)_ | Hard | | | -| 845 | [Longest Mountain in Array](https://leetcode.com/problems/longest-mountain-in-array/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | +| 845 | [Longest Mountain in Array](https://leetcode.com/problems/longest-mountain-in-array/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | | | 015 | [3 Sum](https://leetcode.com/problems/3sum/) | [C++](./C++/3sum.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | | | 021 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Easy | Two Pointer | | +| 28 | [Find the Index of the First Occurence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string) | [C++](./C++/28_first_occurrence_in_a_string.cpp) | _O(1)_ | _O(1)_ | Easy | Two Pointer | |