Notice
250x250
Recent Posts
Recent Comments
Link
넘치게 채우기
[LeetCode] 2185. Counting Words With a Given Prefix 본문
728x90
반응형
https://leetcode.com/problems/counting-words-with-a-given-prefix/description/
leetcode - Counting Words With a Given Prefix
문제 유형: 문자열 처리
문제 난이도: Easy
문제
You are given an array of strings words and a string pref.
Return the number of strings in words that contain pref as a prefix.
A prefix of a string s is any leading contiguous substring of s.
문자열 배열 words와 문자열 pref를 받는다.
pref를 prefix로 가지는 문자열의 개수를 구하시오.
풀이
그대로 구현해주면 된다.
코드
C++
class Solution {
public:
bool isPrefix(const string& word, const string& pref) {
for(int i = 0; i < pref.size(); ++i) {
if(word[i] != pref[i]) return false;
}
return true;
}
int prefixCount(vector<string>& words, string pref) {
int ans = 0;
for(const auto &word : words) {
if(isPrefix(word, pref)) ans++;
}
return ans;
}
};
728x90
반응형
'PS > LeetCode' 카테고리의 다른 글
[LeetCode] 3042. Count Prefix and Suffix Pairs I (0) | 2025.01.08 |
---|---|
[LeetCode] 1408. String Matching in an Array (0) | 2025.01.07 |
[LeetCode] 1769. Minimum Number of Operations to Move All Balls to Each Box (0) | 2025.01.06 |
[LeetCode] 2381. Shifting Letters II (0) | 2025.01.05 |
[LeetCode] 2270. Number of Ways to Split Array (0) | 2025.01.03 |