Question

I am making a method which will return a String[] containing valid combinations of words that differ by one letter. The method takes as String array containing a dictionary of words as the first parameter, and two other strings containing the words one and two respectively as the second and third parameters.

Here is my method:

public static String[] findCombos(String[] dict, String a, String b){
    char[] wordA = a.toCharArray();
    char[] wordB = b.toCharArray();
    int length = wordA.length;
    List<String> validCombos = new ArrayList<String>();
    Arrays.sort(dict);
    //wordA     
    for(int i = 0; i<length; i++){
        char tmp = wordA[i];
        wordA[i] = 0;
        String tmpWordA = new String(wordA).trim();
        //tmpWordA = tmpWordA + wordA.toString().trim();
        if(Arrays.binarySearch(dict, tmpWordA) >= 0){
            int lengthb = wordB.length;
            String tmpWordB = new String(wordB).trim();
            //tmpWordB = tmpWordB + wordB.toString();
            for(int j = 0; j<lengthb; j++){
                tmpWordB = new StringBuffer(tmpWordB).insert(j ,tmp).toString();
                if(Arrays.binarySearch(dict, tmpWordB) >= 0){
                    validCombos.add(tmpWordA + "\\t" + tmpWordB);//combo found
                }else{
                    wordA[i] = tmp;
                }
            }
        }else{
            wordA[i] = tmp;
        }
    }
    //wordB
    int lengthb = b.length();
    for(int i = 0; i<lengthb; i++){
        char tmp = wordB[i];
        wordB[i] = 0;
        String tmpWordB = new String(wordB).trim();
        //tmpWordB = tmpWordB + wordB.toString().trim();
        if(Arrays.binarySearch(dict, tmpWordB) >= 0){
            int lengtha = a.length();
            String tmpWordA = new String(wordA).trim();
            //tmpWordA = tmpWordA + wordA.toString();
            for(int j = 0; j< lengtha; j++){
                tmpWordA = new StringBuffer(tmpWordA).insert(j, tmp).toString();
                if(Arrays.binarySearch(dict, tmpWordA) >= 0){
                    validCombos.add(tmpWordA + "\\t" + tmpWordB);//combo found
                }else{
                    wordB[i] = tmp;
                }
            }
        }else{
            wordB[i] = tmp;
        }
    }
    String[] res = validCombos.toArray(new String[0]);
    return res;
}

The array has been sorted and I am certain that the element in question is in the array, however the search keeps returning a negative number and automatically branching to the else clause. Any ideas? Here is a link to the dictionary:

Dictionary - PasteBin

Was it helpful?

Solution

You are not removing the character at index i, you are replacing the character at index i with 0, that false assumption breaks your algorithm.

Delete a character by index from a character array with StringBuilder

String mystring = "inflation != stealing";
char[] my_char_array = mystring.toCharArray();
StringBuilder sb = new StringBuilder();
sb.append(mystring);
sb.deleteCharAt(10);
my_char_array = sb.toString().toCharArray();
System.out.println(my_char_array);             //prints "inflation = stealing"

The above code removes the exclamation mark from the character array.

Roll your own java function to remove a character from a character array:

String msg = "johnny can't program, he can only be told what to type";

char[] mychararray = msg.toCharArray();
mychararray = remove_one_character_from_a_character_array_in_java(mychararray, 21);
System.out.println(mychararray);

public char[] remove_one_character_from_a_character_array_in_java(
                           char[] original, 
                           int location_to_remove)
{
    char[] result = new char[original.length-1];
    int last_insert = 0;
    for (int i = 0; i < original.length; i++){
        if (i == location_to_remove)
            i++;

        result[last_insert++] = original[i];
    }
    return result;
}

//The above method prints the message with the index removed.

Source: https://stackoverflow.com/a/11425139/445131

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top