简体   繁体   English

在char数组Java中查找单词

[英]Find a word in char array java

I'm trying to find a word from a dictonary list . 我试图从字典中找到一个词。 The letters can be in any order, but any letter can be used only once. 字母可以按任何顺序排列,但是任何字母只能使用一次。 I already developed an algorithm in java on Android, but it doesn't really work. 我已经在Android上的Java中开发了一种算法,但实际上并没有用。 --> all words in the dict list are already lowercased in my cause ->我的原因是字典列表中的所有单词都已经小写

Here is my existing code, but it won't show me the matching words as output, the returned list is always empty. 这是我现有的代码,但是不会显示匹配的单词作为输出,返回的列表始终为空。

    private int matches = 0;
    private ArrayList<String> words;

    private ArrayList<String> check(String charArr0) {
        String charArr = charArr0.toLowerCase();
        char[] cs0 = charArr.toCharArray();
        ArrayList<Character> cs = new ArrayList<>();
        for(char c : cs0)
            cs.add(c);
        all = words.size();
        ArrayList<String> out = new ArrayList<>();

        for(String w0 : words) {
            String w = w0.toLowerCase();
            int len = w.length();
            if(len >= 2) {
                //only if len is 2++
                matches = 0;
                checkNext(cs, 0, w, len);
                //if matches are as high as words lenght, it is fully avaivable
                if(matches >= len)
                    out.add(w);
            }
        }

        return out;
    }

    private void checkNext(ArrayList<Character> cs, int pos, String w, int len) {
        if(pos < len) {
            char twc = w.charAt(pos);
            boolean cont = false;
            int cIdx = -1, curi = 0;
            for(char c : cs) {
                if(c == twc){
                    cont = true;
                    cIdx = curi;
                    break;
                }

                curi += 1;
            }

            if(cont) {
                matches += 1;
                cs.remove(cIdx);
                checkNext(cs, pos + 1, w, len);
            }
        }
    }

The question is, what the error in this code is and how could I possibly get a word from the list in a char array given (any char only used once, order doesn't matter)? 问题是,此代码中的错误是什么?如何才能从给定的char数组(任何char只使用一次,顺序无关紧要)的列表中得到一个单词?

Because you define this rules : 因为您定义了以下规则:

//- The letters can be in any order
//- any letter can be used only once

I would like to sort the character of each word and check if they are equals or not : 我想对每个单词的字符进行排序 ,然后检查它们是否相等:

List<String> dictionaryWords = ...;
String word = "word";
char[] wordChars = word.toCharArray();
Arrays.sort(wordChars);
List<String> foundWords = new ArrayList<>();
for(String w : dictionaryWords){
    if(dictionaryWords.length() != wordChars.length)
        continue;
    char[] wordDictionaryChars = w.toCharArray();
    Arrays.sort(wordDictionaryChars);
    if(Arrays.equals(wordChars, wordDictionaryChars)){
        foundWords.add(w);
    }
}

Consider you have : 考虑您有:

List<String> dictionaryWords = new ArrayList<>(Arrays.asList("drow", "hello"));

This will return : 这将返回:

[drow]

because when you order both word and drow it will gives you [d, o, r, w] 因为当您同时订购worddrow ,它将给您[d, o, r, w]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM