Skip to content

Commit

Permalink
Find the Index of the First Occurence in a String
Browse files Browse the repository at this point in the history
A LeetCode's Algorithm

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.
  • Loading branch information
Tunzeki committed Jun 13, 2023
1 parent fb93a3b commit 70d34ed
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions javascript/strStr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Visit https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/
* for full problem description
*
* 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.
*
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function (haystack, needle) {
// The JavaScript in-built string search method indexOf
// returns the index of the first occurrence of needle in haystack,
// or -1 if needle is not part of haystack, and is all that is needed
// to write the algorithm that solves this problem.
return haystack.indexOf(needle);
};

0 comments on commit 70d34ed

Please sign in to comment.