Hi I'm practicing for my interview and I would like to know if there is a better solution for this problem:
Given a key String key="apple"
and a sorted Array of strings, String[] words ={apple, apples, bananaaple, orangebanana, pearapples}
write a function which return true
when you can concatenate to get the key string using words from the word list, false
if you can't.
my idea is starting with one letter from the key string, and do a binary search from the world list, if exists, find the rest of the string; if not, increase one more letter, and do the same.
my code:
public static void main(String[] args){
String[] words = {"apple", "apples", "bananaaple", "orangebanana", "pearapples"};
String key = "apple";
System.out.println(expanding(words,key));
}
public static boolean expanding(String[] words, String key){
if(key.length()<1){
return false;
}
for(int i=1; i<=key.length(); i++){
String sub = key.substring(0, i);
boolean found =search(words,sub,0,words.length-1);
if(found){ //get next piece
String theSubString =key.substring(i, key.length());
if(theSubString.compareToIgnoreCase("")==0){
return found;
}
boolean re = expanding(words,theSubString );
if(re)
return true;
}
}
return false;
}
public static boolean search(String[] list, String key, int min, int max){
if(min>max){
return false;
}
int mid = (min+max)/2;
if(list[mid].compareToIgnoreCase(key)==0){
return true;
}else if(list[mid].compareToIgnoreCase(key)<0){
return search(list,key,mid+1,max);
}else{
return search(list,key,min,mid-1);
}
}
In my opinion the best case should be O(log n), but not sure the worst case... maybe O(n^2) when can only match one letter at a time.
can anyone give me more ideas?