# Find the Index of the First Occurrence in a String LeetCode Solution

Here, We see Find the Index of the First Occurrence in a String problem Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

## Problem Statement ->

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

```Example 1:
Output: 0
Explanation: "sad" occurs at index 0 and 6.
The first occurrence is at index 0, so we return 0.

Example 2:
Input: haystack = "leetcode", needle = "leeto"
Output: -1
Explanation: "leeto" did not occur in "leetcode", so we return -1.
```

## Find the Index of the First Occurrence in a String Leetcode Solution C++ ->

``````class Solution {
public:
int strStr(string haystack, string needle) {
int m = haystack.size(), n = needle.size();
for (int i = 0; i <= m - n; i++) {
int j = 0;
for (; j < n; j++) {
if (haystack[i + j] != needle[j]) {
break;
}
}
if (j == n) {
return i;
}
}
return -1;
}
};
```Code language: C++ (cpp)```

## Find the Index of the First Occurrence in a String Leetcode Solution Java ->

``````class Solution {
public int strStr(String haystack, String needle) {
int l1 = haystack.length(), l2 = needle.length();
if (l1 < l2) {
return -1;
} else if (l2 == 0) {
return 0;
}
int threshold = l1 - l2;
for (int i = 0; i <= threshold; ++i) {
if (haystack.substring(i,i+l2).equals(needle)) {
return i;
}
}
return -1;
}
}
```Code language: Java (java)```

## Find the Index of the First Occurrence in a String Leetcode Solution JavaScript ->

``````var strStr = function(haystack, needle) {
if (needle.length == 0) return 0;
for (let i = 0; i < haystack.length; i++) {
let k = i, j = 0;
while (haystack[k] == needle[j] && j < needle.length) {
k++, j++;
}
if (j == needle.length) return i;
}
return -1;
};
```Code language: JavaScript (javascript)```

## Find the Index of the First Occurrence in a String Leetcode Solution Python ->

``````class Solution(object):
def strStr(self, haystack, needle):
for i in range(len(haystack) - len(needle)+1):
if haystack[i:i+len(needle)] == needle:
return i
return -1
```Code language: Python (python)```

#### Best Time to Buy and Sell Stock II LeetCode Solution

You are given an integer array prices where prices[i] is the price of a given stock on the ith day. On…

#### Search in Rotated Sorted Array LeetCode Solution

Given the array nums after the possible rotation and an integer target, return the index…

#### Climbing Stairs LeetCode Solution

You are climbing a staircase. It takes n steps to reach the top. Each time you can…

#### Contains Duplicate III LeetCode Solution

You are given an integer array nums and two integers indexDiff and valueDiff. Find a pair of indices (i, j) such that:…

#### Wildcard Matching LeetCode Solution

Given an input string (s) and a pattern (p), implement wildcard pattern matching with support…

#### Contains Duplicate II LeetCode Solution

Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such…
Scroll to Top