import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; public class _819 { public static void main(String[] args) { String arr[] = { "" }; String s = "Bob"; System.out.println(mostCommonWord(s, arr)); } 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; } }// mostCommonWord
原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/245265.html