搜索字母数组中的单词

时间:2015-03-08 23:48:10

标签: java arrays

我有一个名为dictionary的arrayList,其中包含以下字母。 G,T,C,A,N,d,L,E,T,J,A,Q

我希望输出为例, 2candle 3等 该数字是从被搜索数组开始的偏移量。

我希望输出是匹配位置的列表,每个匹配包括从文本开头到找到的字符串的偏移量。 请帮助!!

1 个答案:

答案 0 :(得分:0)

如果我们考虑这类问题,关键是找出所有可能找到单词的地方的可能性。以下是我编写的方法的框架,该方法将用于此过程:

public static String findWords(final char[] characters) {
    String toRet = "";

    // First iterate through every character in the array characters.
    for (int i = 0; i < characters.length; i++) {

        /*
         * Then at each step in this loop, check all possible word
         * combinations to see if it's a word. For example, check and see if
         * characters[i], characters[i+1] forms a word. Then check and see
         * if the word made by adding together characters[i],
         * characters[i+1], and characters[i+2] is a word. Then check and
         * see if the word formed by adding together the characters
         * characters[i], characters[i+1], characters[i+2], characters[i+3]
         * is a word.
         */

        // Doing the above requires a nested loop inside of the original
        // loop.
        for (int j = 0; j < characters.length - i; j++) {

            // When you do find a word that is formed, then go ahead and add
            // to your string toRet the details about the word formed.
        }
    }

    return toRet;
}

这并没有完全回答你的问题,但我希望能