Hello
I have a program I am trying to make which takes in a word pattern (eg; d?ll, h?l?o, go?dby? etc) & searches an 'array containing different words' to find any words in the array that match the input word pattern.
So for example; if I input 'd?ll', the program would output
dell
dill
doll
etc....
Or if I input 'ap??e':
apace
apple
etc.......
My Problem: I am having alot of trouble thinking up an algorithm (way) to search the array to find all the words that match the word pattern.
I know how to determine whether a word has the same 'word pattern' as the input word - see my function below
I am supposed to use a binary search - see my other function, which I know how to do, but could I get some help (algorithms) on how do I mesh the 2 functions to search an array & return all the words that match the input word pattern?
Any suggestions would be greatly appreciated :D
Compare words Function:
bool wordComparision(string s1, string s2) {
if(s1.length() != s2.length())
return false;
if(!s1[0])
return false;
if(s1 == s2) return true;
for(int i = 0; i < (int)s1.length(); i++)
{
// check if all the letters that occur in s1 are
// also present in s2 & visa versa
if(s1[i]!='?') {
if(s1.find(s2[i]) == string::npos)
return false;
if(s2.find(s1[i]) == string::npos)
return false;
}
}
return true;
}
My Binary Search Function:
bool binarySearch(string array[], string word, int begin, int end) {
int mid = end;
while (begin <= end) {
mid = (begin + end) / 2;
if ( word == array[mid] ) { // if the loop gets to this point
return true; // then we have found the word we're looking for
}
else if (word > array[mid]) {
begin = mid + 1;
}
else if (word < array[mid]) {
end = mid - 1;
}
}
return false; // if we get to here the word is not in the dictionary
}