easy 模式
一题目 819Most Common Word
Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer is unique.
Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase.
Example:
Input:
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball"
Explanation:
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"),
and that "hit" isn't the answer even though it occurs more because it is banned.
Note:
1 <= paragraph.length <= 1000
.0 <= banned.length <= 100
.1 <= banned[i].length <= 10
.- The answer is unique, and written in lowercase (even if its occurrences in
paragraph
may have uppercase symbols, and even if it is a proper noun.) paragraph
only consists of letters, spaces, or the punctuation symbols!?',;.
- There are no hyphens or hyphenated words.
- Words only consist of letters, never apostrophes or other punctuation symbols.
二 实现
虽然是easy模式,但是一开始看题花了好一阵,英语不好看起来吃力。而且没看备注,后面吃了苦头。好几次失败了。
中文意思:大概是我们一段字符串paragraph,还有 数组banned words,让我们找出 在 paragraph 里出现最多的 non-banned word。
主要思路是,开始要把paragraph 都变成小写,然后利用 regex split 成 string array;
遍历吧不是banned 放入map计数。求map计数最大的。
public String mostCommonWord(String paragraph, String[] banned) {
if(paragraph == null || paragraph.trim().length()==0){
return "";
}
//先处理为小写,在分割
String[] words = paragraph.toLowerCase().split(" |,|\\.|!|;");
Map<String,Integer> map = new HashMap<String,Integer>();
HashSet<String> bannedSet = new HashSet<>();
for(String s:banned){
bannedSet.add(s);
}
String result ="";
int max =-1;
for(int i=0;i< words.length; i++){
if(words.length==1){
result =words[0];
}
if(!bannedSet.contains(words[i])&&!words[i].equals("") ){
if(map.containsKey(words[i])){
int tmp = map.get(words[i])+1;
if(tmp>max){
max = tmp;
result = words[i];
}
}else{
map.put(words[i] , 1);
}
}
}
if(map.size()==1){
result = map.keySet().iterator().next();
}
return result;
}
Runtime: 25 ms, faster than 24.92% of Java online submissions for Most Common Word.
Memory Usage: 37 MB, less than 79.39% of Java online submissions forMost Common Word.
Complexity Analysis
-
Time Complexity: O(P + B)O(P+B), where PP is the size of
paragraph
and BB is the size ofbanned
. -
Space Complexity: O(P + B)O(P+B), to store the
count
and the banned set.
为啥要用到那么多分隔符,看提示的错误的例子有多变态。各种边界判断,真是服了。
String pram ="L, P! X! C; u! P? w! P. G, S? l? X? D. w? m? f? v, x? i. z; x' m! U' M! j? V; l. S! j? r, K. O? k? p? p, H! t! z' X! v. u; F, h; s? X? K. y, Y! L; q! y? j, o? D' y? F' Z; E? W; W' W! n! p' U. N; w? V' y! Q; J, o! T? g? o! N' M? X? w! V. w? o' k. W. y, k; o' m! r; i, n. k, w; U? S? t; O' g' z. V. N? z, W? j! m? W! h; t! V' T! Z? R' w, w? y? y; O' w; r? q. G, V. x? n, Y; Q. s? S. G. f, s! U? l. o! i. L; Z' X! u. y, Q. q; Q, D; V. m. q. s? Y, U; p? u! q? h? O. W' y? Z! x! r. E, R, r' X' V, b. z, x! Q; y, g' j; j. q; W; v' X! J' H? i' o? n, Y. X! x? h? u; T? l! o? z. K' z' s; L? p? V' r. L? Y; V! V' S. t? Z' T' Y. s? i? Y! G? r; Y; T! h! K; M. k. U; A! V? R? C' x! X. M; z' V! w. N. T? Y' w? n, Z, Z? Y' R; V' f; V' I; t? X? Z; l? R, Q! Z. R. R, O. S! w; p' T. u? U! n, V, M. p? Q, O? q' t. B, k. u. H' T; T? S; Y! S! i? q! K' z' S! v; L. x; q; W? m? y, Z! x. y. j? N' R' I? r? V! Z; s, O? s; V, I, e? U' w! T? T! u; U! e? w? z; t! C! z? U, p' p! r. x; U! Z; u! j; T! X! N' F? n! P' t, X. s; q'"
banned=["m","i","s","w","y","d","q","l","a","p","n","t","u","b","o","e","f","g","c","x"]