Random commentary about Machine Learning, BigData, Spark, Deep Learning, C++, STL, Boost, Perl, Python, Algorithms, Problem Solving and Web Search
I failed this to an interview... so stupid!I think the solution should look like:size_t counter = 0;foreach (char c : stream) { if ( c == "(" ) ++counter; else ( c == ")" ) --counter; if ( counter < 0 ) return false;}if ( counter == 0 ) return true;return false;O(N) in time and O(1) in space
I failed this to an interview... so stupid!
ReplyDeleteI think the solution should look like:
size_t counter = 0;
foreach (char c : stream) {
if ( c == "(" ) ++counter;
else ( c == ")" ) --counter;
if ( counter < 0 ) return false;
}
if ( counter == 0 ) return true;
return false;
O(N) in time and O(1) in space