请设计一个高效的方法,找出任意指定单词在一篇文章中的出现频数。
给定一个string数组article和数组大小n及一个待统计单词word,请返回该单词在文章中的出现频数。保证文章的词数小于等于1000。
c++
class Frequency {
public:
int getFrequency(vector<string> article, int n, string word) {
// write code here
int res = 0;
for (auto str: article){
if(word == str)
res++;
}
return res;
}
};
python
# -*- coding:utf-8 -*-
class Frequency:
def getFrequency(self, article, n, word):
# write code here
return article.count(word)
java
import java.util.*;
public class Frequency {
public int getFrequency(String[] article, int n, String word) {
// write code here
int res = 0;
for (int i=0; i<article.length; i++){
if(article[i].equals(word))
res++;
}
return res;
}
}