TASK #1: Duplicate Removals
You are given a string, $str, consisting of lowercase English letters.
Write a script to return the final string after all duplicate removals have been made. Repeat duplicate removals on the given string until we no longer can.
A duplicate removal consists of choosing two adjacent and equal letters and removing them.
Example:
Input: $str = 'abbcdd'
Output: 'ac'
$str = 'abbcdd';
# (\w) : matches a single word character (letter, digit, underscore) and captures it
# \1 : matches the same character immediately after (the backreference to the first capture)
$str =~ s/(\w)\1//;
$str is now 'acdd'
repeat the command
$str =~ s/(\w)//;
$str is now 'ac'
so a while loop will do the job.
see: section 10.7.1 from https://reiniermaliepaard.nl/perl/part-1/index.php?id=regex
#!/usr/bin/perl use strict; use warnings; sub rm_dup { my $str = shift; do { $str =~ s/(\w)\1//; } while ($str =~ /(\w)\1/); return $str; } # Tests printf "%s\n", rm_dup('abbaca'); # ca printf "%s\n", rm_dup('azxxzy');# ay printf "%s\n", rm_dup('aaaaaaaa');# '' printf "%s\n", rm_dup('aabccba');# a printf "%s\n", rm_dup('abcddcba')# ''