Say you have an array of words
String[] arr = {"i", "a", "am", "good", "program", "gram"};
And a given key
String key = "iamgood";
boolean allWordsFound(arr, key){
// should return true if all possible words from array make up the key.
// e.g. keys matched {"i", "am", "good"};
}
My approach :
boolean workbreak(String[] arr, String key) {
StringBuilder tempStr = new StringBuilder();
String[] possibleValues;
int count = 0;
for(i = 0; i < key.length()- 1; i++){
tempStr.append(key[i]);
for(j =0; j < arr.length(); j++){
tempStr == arr[j]{
return true;
//possibleValues[count] = tempStr;
//count++;
}
}
}
}
I am unable to figure out an approach. I do not need code but can you please suggest an algorithm.