Skip to content

Latest commit

 

History

History
executable file
·
20 lines (13 loc) · 621 Bytes

File metadata and controls

executable file
·
20 lines (13 loc) · 621 Bytes

301. Remove Invalid Parentheses

we only focus () and make the input valid.

std use bfs.

first how to dertermin it's valid? )( => false; which means we let each left bracket ++ and right bracket --; if we get a negative value in this process which means we get a ) and no (.

and the bfs will delete all ( or ) in any times.

 string str = t.substr(0, i) + t.substr(i + 1);
                if (!visited.count(str)) {
                    q.push(str);
                    visited.insert(str);
                }

if we do not face this string before, we will delete this bracket and push it into the queue.