CodingTest/LeetCode

[LeetCode] 28. Find the Index of the First Occurrence in a String, Easy

뜸부깅 2025. 3. 12. 11:12
반응형

1. 문제

  • 문자열 haystack, needle이 주어질 때, haystack 문자열 안에 needle이 존재하면, 시작 인덱스를 반환하고 없으면 -1을 반환하라.

2. 해결

function strStr(haystack: string, needle: string): number {    
    return haystack.indexOf(needle);
};
  • indexOf 함수를 이용해 해결할 수 있다.