String Construction

  • + 0 comments

    Perl:

    sub existElement {
        my $element = shift;
        my $arr = shift;
        for (my $i = 0; $i <= scalar(@$arr) - 1; $i++){
            if ($arr->[$i] eq $element) {
                return ($arr->[$i] eq $element) ? 1 : 0;
                last;
            }        
        }
    }
    
    sub stringConstruction {
        my @s = split("", shift);
        my $res = [0];
        my $cnt = 0;
        foreach my $el (@s) {
            if (!existElement($el, $res)) {
                push(@$res, $el);
                $cnt++;
            }        
        }
        return $cnt;
    }