RosettaCodeData/Task/Balanced-brackets/Perl/balanced-brackets-4.pl

9 lines
181 B
Perl
Raw Permalink Normal View History

2015-02-20 00:35:01 -05:00
sub balanced {
my $depth = 0;
for (split //, shift) {
if ($_ eq '[') { ++$depth }
elsif ($_ eq ']') { return if --$depth < 0 }
}
return !$depth
}