반응형
https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/
Find the Index of the First Occurrence in a String - LeetCode
Can you solve this real interview question? Find the Index of the First Occurrence in a String - 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. Example 1: I
leetcode.com
O(n + m) 알고리즘을 직접 작성해도 되겠지만.. 손쉽게 STL을 사용하여 풀어주었습니다.
class Solution {
public:
int strStr(string haystack, string needle) {
auto idx = haystack.find(needle);
return idx == string::npos ? -1: idx;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 1345. Jump Game IV (0) | 2023.03.05 |
---|---|
LeetCode 2444. Count Subarrays With Fixed Bounds (0) | 2023.03.04 |
LeetCode 443. String Compression (0) | 2023.03.02 |
LeetCode 912. Sort an Array (0) | 2023.03.01 |
LeetCode 652. Find Duplicate Subtrees (0) | 2023.02.28 |