如何在Trie中找到特定的单词

时间:2017-02-06 12:50:05

标签: java

我想在这种表达式中找到以A开头并以E结尾的3个字母单词:
A?E

我的程序是字典,我正在使用此代码:

    public ArrayList<String> Search(String word){

    Node current = root;
    ArrayList<String> result = new ArrayList<>();

    while(current != null){
        String st = "";
        for(int i = 0; i < word.length(); i++){
            if(current.SubNode(word.charAt(i)) != null) {
                current = current.SubNode(word.charAt(i));
                st+= current;
            }
            if(word.charAt(i) == '?'){
                current = current.SubNode(word.charAt(i));
                st+= current;
            }
        }
        if (current.prefixes == true)
            result.add(st);
    }
    return result;
}

但它不起作用

1 个答案:

答案 0 :(得分:0)

以下是如何使用Regex查找它的示例。只需通过您的收藏进行迭代。

String input = "AoE";

    Pattern p = Pattern.compile("(A*.*E)");
    Matcher m = p.matcher(input);

    while (m.find()) {
        System.out.println("m.length() = " + m.group().length());
        if (m.group().length() == 3){
            System.out.println("Found a " + m.group() + ".");
        }
    }