題目描述
在中國,形容夫妻恩愛的詞彙中,大家用的比較多的就是“夫妻相”。所謂“夫妻相”,就是兩個人看上去比較般配,長相、身材等某些方面有一定的相似度。本題則另闢蹊徑,從人的姓名維度,以字母重複個數來尋找最具“夫妻相”的人。題目中預先給定一組女士的姓名拼音。輸入男士的姓名拼音(拼音中間可以有空格,字母全部小寫),依預先給定姓名拼音的先後遍歷所有姓名,輸出字母重複數最多的女士姓名。
規則1:如果字母重複數最多的女士有多位相同,則以最先匹配的女士做爲最具“夫妻相”的人選。
規則2:人名中的相同字母,按重複一次處理。例如:li ling 與li lei 重複的字符個數爲2,而不是4。
預置女士名單(先後循序必須保證):
"wang fei","zhang man yu","zhang zhi yi","li li","li xiao man","li yu cun","yang ni","xiao tong","li lei","zhang san"
輸入
輸入一個男士姓名
輸出
輸出最具“夫妻相”的女士姓名
樣例輸入
li si
樣例輸出
li li
提示
地區
公共
產品線
公共
階段
入職前練習
public static void main(String[] args) {
String input="wang fei,zhang man yu,zhang zhi yi,li li," +
"li xiao man,li yu cun,yang ni,xiao tong,li lei,zhang san";
String[] femaleNames=input.split(",");
getHusbandWifeLooks(femaleNames);
}
public static void getHusbandWifeLooks(String[] femaleNames){
Scanner sc=new Scanner(System.in);
String maleName=sc.nextLine();
LinkedHashSet<Character> ls=new LinkedHashSet<Character>();
LinkedHashMap<String,Integer> lhm=new LinkedHashMap<String,Integer>();
int count=0;
for(int i=0;i<femaleNames.length;i++){
char[] c=femaleNames[i].toCharArray();
for(int j=0;j<c.length;j++){
if(c[j]!=32)
ls.add(c[j]);
}
for(char cc:ls){
if(maleName.contains(String.valueOf(cc))){
count++;
}
}
ls.clear();
lhm.put(femaleNames[i], count);
count=0;
}
System.out.println(lhm);
List<Map.Entry<String,Integer>> li=new ArrayList<Map.Entry<String,Integer>>(lhm.entrySet());
Collections.sort(li, new Comparator<Map.Entry<String,Integer>>(){
@Override
public int compare(Entry<String, Integer> o1,Entry<String, Integer> o2) {
return o2.getValue()-o1.getValue();
}});
System.out.println(li.get(0).getKey());
}