Define the array record
by boolean[] record = new boolean[s.length()];
with a lowercase b
for b
oolean. That will do the trick.
Your variable left
is useless. It makes your code harder to read. Please when you ask question here, first describe your algorithm in plain english fist and add comments. You might have realized that left
is useless by yourself.
In Java, you shouldn't use the class Stack
, it's better to use a class that implements the Deque
interface. Have a look at ArrayDeque
.
It is a bad coding habit to test equality between a boolean variable and a boolean constant. You should replace line like if (a == false)
by if (!a)
and lines like if (b == true)
by if (b)
.
Your code won't give the right answer to the problem. Try to figure out why before reading the solution below.
for (int i = 0; i < record.length; i++) {
if (record[i]) {
count++;
result = Math.max(result,count);
} else {
count = 0;
}
}