猜字谜

alt

源码 Java


import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

public class GuessWord {

	public Input input;

	@BeforeEach
	public void init() {
		input= new Input("bdni,wooood\nbind,wrong,wood");
	}

	@Test
	public void test() {
		String string = input.nextLine();
		String[] words = string.split(",");
		String[] answers = input.nextLine().split(",");

		List<String> wordList = new ArrayList<>();
		List<String> answerList = new ArrayList<>();

		for (String word : words) {
			wordList.add(dealWord(word));
		}

		for (String answer : answers) {
			answerList.add(dealWord(answer));
		}

		List<String> result = new ArrayList<>();
		for (int i = 0; i < wordList.size(); i++) {
			int index = findWord(answerList, wordList.get(i));
			if (index != -1) {
				result.add(answers[i]);
			} else {
				result.add("not found");
			}
		}

		System.out.println(String.join(",", result));


	}

	public int findWord(List<String> lists , String word) {
		for (int i = 0; i < lists.size(); i++) {
			if (lists.get(i).equals(word)) {
				return i;
			}
		}
		return -1;

	}

	public String dealWord(String word) {
		TreeSet<Character> set = new TreeSet<>();
		for (int i = 0; i < word.length(); i++) {
			set.add(word.charAt(i));
		}
		Character[] array = set.toArray(new Character[set.size()]);
		StringBuilder answer = new StringBuilder();
		for (int i = 0; i < array.length; i++) {
			answer.append(array[i]);
		}
		return answer.toString();
	}
}

#牛客创作赏金赛##我的求职思考#
全部评论

相关推荐

点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务