# 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++ ->

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```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)```

#### Longest Palindromic Substring LeetCode Solution

Given a string s, return the longest palindromic substring in s. A palindrome is a string which reads the…

#### 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…

#### Combination Sum LeetCode Solution

Given an array of distinct integers candidates and a target integer target, return a list…

#### Permutations II LeetCode Solution

Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in…

#### House Robber LeetCode Solution

Given an integer array nums representing the amount of money of each house, return the…

#### Single Number LeetCode Solution

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.