HackerRank in a String!

  • + 0 comments

    Perl solution:

    sub hackerrankInString {
        my $str = shift;
        my @s = split("", $str);
        my @t = split("", "hackerrank");
        my $pos = -1;
        my @res_arr;
        
        for (my $i = 0; $i <= scalar(@t) - 1; $i++) {
            for (my $j = 0; $j <= scalar(@s) - 1; $j++) {
                if ($t[$i] eq $s[$j] && $pos < $j) {
                    $pos = $j;
                    push(@res_arr, $s[$j]);
                    last;
                }            
            }
        }
    
        if (join("", @res_arr) eq "hackerrank") {
            return "YES";
        } else { return "NO"; }
    }