Why is this giving WA?


#1

Basically what I am trying to do is , as soon as I find a prefix of the given string which is available in the dictionary, i try to break the string there and check if it is possible to break the suffix from there onwards in a similar fashion .
‘’’
set dictionary;
int n;
bool check(int pos, string A){
// returns if we can break the suffix from pos into dictionary words
if(pos==n){
return 1;
}
bool ans = false;
string prefix = “”;
for(int i=pos;i<n;i++){
prefix += A[i];
if(dictionary.count(prefix)){
ans = (ans || check(i+1, A));
// if(ans==1){
// cout<<“pos = “<<pos<<”\n”;
// }
}
}

return ans;

}

int Solution::wordBreak(string A, vector &B) {
n = A.length();
dictionary.clear();
dictionary.insert(B.begin(), B.end());
return check(0,A);
}

‘’’