The Find the Index of the First Occurrence in a String solutions are below.
15 Jun 2024•Easy•C++ / Python / TypeScript
Find the Index of the First Occurrence in a String
C++, Python and TypeScript solutions for Find the Index of the First Occurrence in a String.
Solution files
Python•find-the-index-of-the-first-occurrence-in-a-string/synced-solution.py
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
return haystack.find(needle)
C++•find-the-index-of-the-first-occurrence-in-a-string/synced-solution.cpp
class Solution {
public:
int strStr(string haystack, string needle) {
size_t pos = haystack.find(needle);
return pos == string::npos ? -1 : pos;
}
};
TypeScript•find-the-index-of-the-first-occurrence-in-a-string/synced-solution.ts
function strStr(haystack: string, needle: string): number {
return haystack.indexOf(needle);
}