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