简体   繁体   中英

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. --> 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)?

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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