# 【华为OD机试真题2023 JAVA】猜字谜

2,233 阅读2分钟

## 猜字谜

### 解题思路：

1. 判断变换顺序后是否一致：将两个字符串变为数组后进行排序，如果排序后的数组一致，则表示变序后一致
2. 去重后是否一致：将两个字符串去重后加入列表中，如果两个列表一致，则表示去重后一致。
3. 1和2都符合，则添加字符串“not found”

## 代码块

``````
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String[] mimian = sc.nextLine().split(",");
String[] midi = sc.nextLine().split(",");

List<String> resList = new ArrayList<>();
for(int i=0; i<mimian.length; i++){
String mm = mimian[i];  //谜面
boolean isFound = false;    //是否存在谜底
for(int j=0; j<midi.length; j++){
String md = midi[j];    //谜底
if(bianxu(mm, md)){
resList.add(md);
isFound = true;
}else if(quchong(mm, md)){
resList.add(md);
isFound = true;
}
}
if(!isFound){
resList.add("not found");
}
}

String res = "";
for(int i=0;i<resList.size();i++){
res += resList.get(i) + ",";
}
System.out.println(res.substring(0,res.length()-1));
}

/**
* 变序后谜面和谜底是否一致
* @param miman
* @param midi
* @return
*/
public static boolean bianxu(String miman, String midi){

String[] mmStrs = miman.split("");
Arrays.sort(mmStrs);
String[] midiStrs = midi.split("");
Arrays.sort(midiStrs);

if(Arrays.equals(mmStrs, midiStrs)){
return true;
}

return false;
}

/**
* 去重后谜面与谜底是否一致
* @param miman
* @param midi
* @return
*/
public static boolean quchong(String miman, String midi){

List<Character> mmList = new ArrayList<>();
for(int i=0; i<miman.length(); i++){
char c = miman.charAt(i);
if(!mmList.contains(c)){
mmList.add(c);
}
}

List<Character> midiList = new ArrayList<>();
for(int i=0; i<midi.length(); i++){
char c = midi.charAt(i);
if(!midiList.contains(c)){
midiList.add(c);
}
}

if(mmList.equals(midiList)){
return true;
}
return false;

}

}
``````