笔试:单词问题

来源:互联网 发布:json 数组如何表示 编辑:程序博客网 时间:2024/06/11 18:33

1

题目: 给出包含一些单词作为关键字和只在一个字母上不同的一列单词作为关键字的值,输出那些具有至少minWords个通过1个字母替换得到的单词的单词。
key ——value
wine—— wide、wife、wipe、wink、wins。。

我们知道标准库中Map接口,接受 《key, value》数据。其中关键字key唯一,值value可以不唯一。

    public static void printHighChangeables(Map<String, List<String>> adjacentWords, int minWords) {        for (Map.Entry<String, List<String>> entry : adjacentWords.entrySet()) {            List<String> words = entry.getValue();            if (words.size() >= minWords) {                System.out.print(entry.getKey() + " )" + words.size() + "):");                for (String w : words)                    System.out.print(" " + w);                System.out.println();            }        }    }

2

检测两个单词是否只在一个字母上不同

public static boolean oneCharOff(String word1, String word2){    if(word1.length() != word2.length())        return false;    int diffs = 0;    for(int i = 0; i < word1.length(); i++)        if(word1.charAt() != word2.charAt())            if(++diffs > 1)                return false;    return diffs == 1;}
0 0
原创粉丝点击