-2

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

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Invictus
  • 4,028
  • 10
  • 50
  • 80
  • `str_size+1` and `for(int i =1;i<=str_size;++i)` is not the correct way. Array indexes are zero-based, not one-based. Stop being part of a [cargo cult](https://en.wikipedia.org/wiki/Cargo_cult_programming) and invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn C++ properly. Oh, and stop using sites like that to learn the basics of programming or languages, that's just not their purpose. – Some programmer dude Aug 05 '23 at 08:08
  • 1
    Thanks looks like i figured out the issue by the way which is std::fill(dict, dict+str_size,false); should have std::fill(dict, dict+str_size+1,false); Agree with your thoughts but sometimes starting with 1 gives more clarity in some cases while composing the algo. – Invictus Aug 05 '23 at 08:12
  • Why a dynamic array in the first place? `std::vector dict(str_size+1, false);` will also work, and you cannot forget to `delete[]` it at the end. – BoP Aug 05 '23 at 08:23
  • @BoP Vector is also a heap allocation in general. Yes, agree with delete thing. – Invictus Aug 05 '23 at 11:58

1 Answers1

1

you are checking if dict[j] is true and if j is equal to str_size, however,the problem occurs when j becomes equal to str_size, so I think you must modify the loop condition to j < str_size instead of j <= str_size because it ensures you the j remains within the bounds of the dict array!

so fix it like below :

if ((dict[j] == true) && (j == str_size - 1))
Freeman
  • 9,464
  • 7
  • 35
  • 58