I am trying to solve a problem here https://leetcode.com/problems/word-break/ . My code looks like below:-
bool existsInDict(string s, vector<string>& wordDict)
{
if(std::find(wordDict.begin(),wordDict.end(),s) != wordDict.end())
{
return true;
}
return false;
}
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
int str_size = s.length();
if(str_size == 0)
return true;
bool *dict = new bool[str_size+1];
std::fill(dict, dict+str_size,false);
for(int i =1;i<=str_size;++i)
{
if(dict[i]==false && existsInDict(s.substr(0,i),wordDict))
{
dict[i] = true;
}
if(dict[i]==true)
{
if(i==str_size)
return true;
for(int j=i+1;j<=str_size;++j)
{
if((dict[j]==false) && existsInDict(s.substr(i+1,j-i),wordDict))
{
dict[j] = true;
}
if((dict[j]==true) && (j == str_size))
{
return true;
}
}
}
}
return false;
}
};
This gives me a error Line 40: Char 25: runtime error: load of value 190, which is not a valid value for type 'bool' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:49:25
I am not sure what is wrong here as both my checks in the if loop on that line have a bool outcome. Can someone help me understand it ?
Thanks