알고리즘/LeetCode

[LeetCode/easy] strStr()

녕이 2022. 8. 12. 15:49
728x90

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에 문자열 2가 포함되어있다면 어디서부터 시작하는지를 묻는 문제, string::npos를 사용하면 된다.

 

int strStr(string haystack, string needle) {
    int answer = 0;
    
    size_t nPos = haystack.find(needle);
    if(nPos == string::npos) return -1;
    else answer = nPos;
    return answer;
}

 

 

 

728x90