class Solution {
public static String mostCommonWord(String s, String[] arr) {
s = s.toLowerCase();
Set<String> set = new HashSet();
Map<String, Integer> map = new HashMap<>();
for (String i : arr) {
set.add(i);
}
StringBuffer str = new StringBuffer(s);
String st = "";
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) >= 'a' && str.charAt(i) <= 'z')) {
st += str.charAt(i);
if (st.length() == str.length()) {
return st;
}
if (i == str.length() - 1) {
if (st.length() != 0) {
if (!set.contains(st) && st != null) {
if (map.containsKey(st)) {
map.put(st, map.get(st) + 1);
} else {
map.put(st, 1);
}
}
st = "";
}
}
} else {
if (st.length() != 0) {
if (!set.contains(st) && st != null) {
if (map.containsKey(st)) {
map.put(st, map.get(st) + 1);
} else {
map.put(st, 1);
}
}
st = "";
}
}
}
String ss = "";
int max = 0;
for (String i : map.keySet()) {
if (map.get(i) > max) {
max = map.get(i);
ss = i;
}
}
return ss;
}
}
想麻烦了,都是泪啊!!