[leetcode, JS] 28. Find the Index of the First Occurrence in a String

mxxn·2023년 8월 8일
0

leetcode

목록 보기
11/198

문제

문제 링크 : Find the Index of the First Occurrence in a String

풀이

/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */
var strStr = function(haystack, needle) {
    return haystack.includes(needle)
    ? haystack.indexOf(needle)
    : -1 
};
  1. haystack에 needle이 포함되어 있는지 확인하여
  2. 있으면 indexOf 값, 없다면 -1을 return
  • Runtime 48 ms, Memory 41.4 MB
profile
내일도 글쓰기

0개의 댓글