Simple String compression and comparison
===

I'm trying to compare two strings and as output I would like a count of consecutive identical characters, and if the character is different, just the char from the second string. I have a working recursive implementation, but I can't figure out how to add consecutive counts together

Code:

    use strict;
    use warnings;
    use Data::Dumper;
    $Data::Dumper::Indent = 0;
    $Data::Dumper::Terse  = 1;
    
    my $str1 = "aaaaaaaaaaaabbbbbbbbbbbccccccccdddddddddddeeeefffffff";
    my $str2 = "aaaaaaaaaaaabbbbbbbbbbbccccccccxxxxxxxddxxeeeefffffff";
    
    sub find_diff {
        my ( $a, $b ) = @_;
        my @rtn = ();
        my $len = length $a;
        my $div = $len / 2;
        if ( $div < 1 ) {
            return $b;
        }
        my $a_1 = substr $a, 0, $div;
        my $b_1 = substr $b, 0, $div;
        if ($a_1 eq $b_1) {
             push @rtn, length $a_1;
        }
        else {
            push @rtn, find_diff( $a_1, $b_1 );
        }
        my $a_2 = substr $a, $div;
        my $b_2 = substr $b, $div;
        if ($a_2 eq $b_2) {
            push @rtn, length $a_2;
        }
        else {
            push @rtn, find_diff( $a_2, $b_2 );
        }
        return @rtn;
    }
    
    print Data::Dumper::Dumper( \@{[ find_diff('xaabbb', 'aaabbc' ) ]} ) . "\n";
    print Data::Dumper::Dumper( \@{[ find_diff('aaabbb', 'aaabbc' ) ]} ) . "\n";
    print Data::Dumper::Dumper( \@{[ find_diff( $str1, $str2 ) ]} ) . "\n";


Output:

    ['a',2,1,1,'c']
    [3,1,1,'c']
    [26,3,1,1,'x','x','x','x','x','x','x',1,1,'x','x',4,7]

Desired Output:

    ['a',4,'c']
    [5,'c']
    [31,'x','x','x','x','x','x','x',2,'x','x',11]

Of course I can split the characters into an array with `unpack` and then count consecutive matches fairly easily, but I want to try a divide-and-conquer approach so I can compare performance.

Thanks!

