Why is this giving WA?


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
return 1;
bool ans = false;
string prefix = “”;
for(int i=pos;i<n;i++){
prefix += A[i];
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.insert(B.begin(), B.end());
return check(0,A);