In
for(int i = 0; i < count.size();i++){
if(count[i] == (count[i+1])){
return false;
}
}
i
will reach count.size()-1
and count[i+1]
becomes count[count.size()-1+1]
which is count[count.size()]
and out of range.
A better way to write this loop is
for(int i = 1; i < count.size(); i++){
if(count[i-1] == (count[i])){
return false;
}
}
It starts one later and iterates one less. The exit condition prevents the loop from ever entering if there are less than 2 elements in count
making underflowing the buffer impossible.
Note: This answer does not take into account the correctness of the algorithm.
CLICK HERE to find out more related problems solutions.