一、题目描述
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.
Input:
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball"
二、题解
方法一:map
* 繁琐解法:出现的特例有很多,比如:
- 字符串 paragraph 末尾没有
.
,逗号,
后面有空格。
这样处理起来会很麻烦。我们其实可以用 String 内部的 replace 方法,将干扰字符全部替换为空格 ' '
。
public String mostCommonWord(String para, String[] banned) {
Set<String> ban = new HashSet<>();
for (String b : banned)
ban.add(b.toLowerCase());
Map<String, Integer> map = new HashMap<>();
int l = 0, r = 0, N = para.length();
while (r < N) {
if (!Character.isLetter(para.charAt(r))) {
String sub = para.substring(l, r).toLowerCase();
if (!ban.contains(sub)) {
map.put(sub, map.getOrDefault(sub, 0) + 1);
}
r++; l = r;
while (r < N && !Character.isLetter(para.charAt(r))) {
r++;
}
l=r;
}
r++;
}
String res = "";
int max = -1;
for (String key : map.keySet()) {
if (map.get(key) > max) {
res = key;
max = map.get(key);
}
}
return res;
}
* 更正后的代码:,坑超多的:
- 将所有非字母的字符替换成空格。
- 每次替换,需要都会产生一个新字符串,所以要重新对引用赋值。
- 替换后,可能会出现空格字符串,所以要判断长度是否大于 0.
public String mostCommonWord(String para, String[] banned) {
Map<String, Integer> map = new HashMap<>();
Set<String> ban = new HashSet<>(Arrays.asList(banned));
for (char c : para.toCharArray()) {
if (!Character.isLetter(c))
para = para.replace(c, ' ');
}
para = para.toLowerCase();
String[] words = para.split(" ");
for (String word : words) {
if (word.length() > 0 && !ban.contains(word))
map.put(word, map.getOrDefault(word, 0) + 1);
}
int max = -1;
String res = "";
for (String word : map.keySet()) {
if (map.get(word) > max) {
max = map.get(word);
res = word;
}
}
return res;
}
复杂度分析
- 时间复杂度: O ( n ) O(n) O(n),
- 空间复杂度: O ( n ) O(n) O(n),
方法二:
复杂度分析
- 时间复杂度: O ( ) O() O(),
- 空间复杂度: O ( ) O() O(),