class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
HashSet<String> banset = new HashSet<String>();
for(int i=0; i<banned.length;i++) {
banset.add(banned[i]);
}
String msword="";
int mscount=0;
HashMap<String,Integer> count = new HashMap<String,Integer>();
String[] words = paragraph.split("[!?',;. ]");
for(int i=0; i<words.length;i++) {
String key = words[i].toLowerCase();
if(key.length()>0) {
if(!banset.contains(key)) {
count.put(key,count.getOrDefault(key, 0)+1);
}
if(count.get(key)>=mscount) { //这一行出错,说是空指针异常,不懂
mscount = count.get(key);
msword = key;
}
}
}
return msword;
}
}
原因:
java.lang.NullPointerException
at line 19, Solution.mostCommonWord
at line 54, DriverSolution.helper
at line 87, Driver.maind