#!/usr/bin/perl use strict; use warnings; my ($str1, $str2) = ("ABCDE", "BBBBB"); print join '', In_Common2($str1, $str2); sub In_Common2 { my ($str1, $str2) = @_; (my $common = reverse $str2) =~ s/[^$str1]|(.)(?=.*\1)//g; return scalar reverse $common; } print $str2; print join '', In_Common2($str1, $str2);