알고리즘 풀이/leetcode

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

mxxn 2023. 9. 25. 10:25

문제

문제 링크 : 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