1

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

3
  • 1
    So what's the question? Commented Aug 5, 2018 at 10:46
  • I updated the question @Malt Commented Aug 5, 2018 at 10:49
  • @Malt I just wanted someone to improve the code or give me a suggestion for another method how to achieve this Commented Aug 5, 2018 at 10:55

1 Answer 1

2

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]

Sign up to request clarification or add additional context in comments.

4 Comments

We also can improve its performance by having the separate list for each length and keep them on the map which helps to reduce for loop cycle. Also can think about having an already sorted list to ignore the sort operation as well.
And what if "drow" contains more than these 4 chars
Yes @ShivangAgarwal I agree with you, I think for that also, this up to the user in his case I would store for each word a sorted word which can be used in search
@Deplover what did you mean by what if "drow" contains more than these 4 chars? do you want to accept words that are not the same length with your word?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.