I have voiceResults in an array to search for a contact:
Ben McDonald
Ben MacDonald
Ken McDonald
Ken MacDonald
I have established the potentialMatches (example) in another array:
Ben McDonald
Benjamin MacDonald
Donna McBlead //anagram
Ben Mad
abcdelmno //occurrences in alphabetical order
onmledcba //occurrences in reverse alphabetical order
completely Random
a cannon
BK Lounge
My goal is to establish which is the most likely contact the user wanted to view.
When looping through the two arrays, I want to use similar logic to the game Mastermind, where I can see if a letter is 'right but in the wrong place' or 'right and in the right place'. I can then compare it to the .length() of the element and get a floating percentage of letter matches and exact position matches.
To do the above, I not only need to loop between elements of the arrays, I then need to break the elements up by their letters and compare those individual element letters. To establish the Mastermind/anagram logic, I would need to remove letters that are matches until I'm left with the letters that don't match and again compare that amount to the original length to get a percentage.
Looking at the example array data above, I also need to perform this in reverse and spit first names and last names.
For each array I started with the following:
ArrayList<String> voiceResults = new ArrayList<String>();
ListIterator<String> itr = voiceResults .listIterator();
Arrays.asList(voiceResults.toArray());
while (itr.hasNext()) {
sid = itr.nextIndex();
element = itr.next();
sidpass = sid.toString();
rawpass = element.toString().toLowerCase();
rawpass.trim();
hcs = rawpass.split("\\s");
hnc = hcs.length;
if (hnc == 2) {
fn = hcs[0]; //first name
ln = hcs[1]; //last name
fn = fn.replaceAll("[^a-z]", ""); //remove punctuation
ln = ln.replaceAll("[^a-z]", "");
}
I post the above, but I'm certain it's not the correct starting method.
Reading through many examples of anagram checks and algorithms, they vary hugely and use for and while loops, hashmaps, hash-tables, histograms, floating values etc.
I hold my hands up, I'm at a total loss of which is the best/fastest/most practical method to initially perform these loops, inner loops, inner element loops...
It would be greatly appreciated if I could have some suggestions of how the loops should be structured to begin with.
Further suggestions/examples/links of the letter comparisons and reverse iteration would be fantastic. Hopefully I can piece everything together then.
Finally, how should I store these percentages that relate to the elements??
I thank you in advance.
PLEASE NOTE: Although the example data may suggest otherwise, I have already used a loop and .contains() .matches() etc etc.