华为OD机试-猜字谜-2022Q4 A卷-Py/Java/JS

小干设计了一人简单的清字谈游戏,游戏的迷面是一人错误的单词,比如nwes,玩家需要猜出谈底库中正确的单词。猜中的要求如·
对于某个谜面和谜底单词,满足下面任一条件都表示猜中:
1)变换顺序以后一样的,比如通过变换w和e的顺序,“nwes”跟“news”是可以完全对应的
2)字母去重以后是一样的,比如“woood”和“wood”是一样的,它们去重后都是“wod”请你写一个程序帮忙在谜底库中找到正确的谜底。迷面是多个单词,都需要找到对应的谜底,如果找不到的话,返"not found"

输入描述
1、谜面单词列表,以","分隔
2、谜底库单词列表,以","分隔
输出描述:
匹配到的正确单词列表,以“,”分隔

如果找不到,返回"not found"
补充说明:
1、单词的数量N的范围: 0 < N < 1000
2、词汇表的数量M的范围: 0 < M< 1000
3、单词的长度P的范围: 0< P< 203
4、输入的字符只有小写英文字母,没有其它字符

示例1
输入:
conection
connection,today
输出:
connection
示例2
输入:
bdni,wooood
bind,wrong,wood
输出:
bind,wood

Java 代码

import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
 
class Main {
	public static void main(String[] args) {
        // 处理输入
        Scanner in = new Scanner(System.in);
        String[] problems = in.nextLine().split(",");
        String[] answers = in.nextLine().split(",");
 
        List resList = new ArrayList<>();
        for(int i=0; i str_set = new HashSet<>();
        for (char c : input_str.toCharArray()) 
            str_set.add(c);
        return str_set.toString();
    }
 
}

Python代码

import functools
import sys
from collections import Counter, defaultdict
import copy
from itertools import permutations
import re
import math
import sys
 
 
 
#处理输入
problems = input().split(",")
answers = input().split(",")
 
resList = []
for i in range(len(problems)):
    problem_str = "".join(sorted(set(problems[i])))
    flag = False
    for j in range(len(answers)):
        answer_str = "".join(sorted(set(answers[j])))
        # 变换顺序 + 去重对比
        if(answer_str == problem_str):
            resList.append(answers[j])
            flag = True
    if(not flag):
        resList.append("not found");
 
 
print(",".join(resList))

JS代码

function main(problem_str, answer_str) {
    const result = [];
    
    for (let problem of problem_str.split(",")) {
        const str1 = [...new Set(problem)].sort().join("");
        let flag = false;
    
        for (let answer of answer_str.split(",")) {
            const str2 = [...new Set(answer)].sort().join("");
            if (str1 === str2) {
                result.push(answer);
                flag = true;
            }
        }
    
        if (!flag) {
            result.push("not found");
        }
    }
    
    console.log(result.join(""))
}
 
 
main("conection","connection,today")

你可能感兴趣的:(java,javascript,华为,python)