본문 바로가기
LeetCode/코딩 테스트 스터디 2주차

[LeetCode] 28. Implement strStr() 풀이 (JS)

by inwoo1324 2022. 7. 17.

0. 문제

https://leetcode.com/problems/implement-strstr/

 

Implement strStr() - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

두개의 문자열이 주어진다. 두 번째 문자열에 해당하는 첫 번째 문자열에 인덱스를 반환한다.

#1
Input: haystack = "hello", needle = "ll"
Output: 2
#2
Input: haystack = "aaaaa", needle = "bba"
Output: -1

 

1.언어

자바스크립트(JavaScript)

 

2. 문제 풀이

var strStr = function(haystack, needle) {
    return haystack.indexOf(needle)
};
Runtime: 66 ms, faster than 88.94% of JavaScript online submissions for Implement strStr().
Memory Usage: 41.9 MB, less than 72.09% of JavaScript online submissions for Implement strStr().
 

indexf 함수 사용