프로그래머스 181871 문자열이 몇 번 등장하는지 세기

1 개요[ | ]

프로그래머스 181871 문자열이 몇 번 등장하는지 세기

2 C++[ | ]

#include <string>
#include <vector>

using namespace std;

int solution(string myString, string pat) {
    int pos = 0;
    int cnt = 0;
    while(true) {
        pos = myString.find(pat, pos);
        if(pos == string::npos) {
            break;
        }
        cnt++;
        pos++;
    }
    return cnt;
}
#include <string>
#include <vector>

using namespace std;

int solution(string myString, string pat) {
    int cnt = 0;
    int len = pat.length();
    for(int i=0; i<myString.length()-len+1; i++) {
        if(myString.substr(i, len) == pat) {
            cnt++;
        }
    }
    return cnt;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}