Advertisement
cd62131

delete duplicate in string

Dec 1st, 2018
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.64 KB | None | 0 0
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use feature q(say);
  5. sub delete_duplicate($);
  6. sub delete_duplicate($) {
  7.   my $s = shift;
  8.   my $len = length $s;
  9.   my $threshold = 5;
  10.   my $t;
  11.   my $found = 0;
  12.   for (my $i = $len / 2; !$found && $threshold <= $i; --$i) {
  13.     for (my $j = 0; $i + $j <= $len; ++$j) {
  14.       $t = substr $s, $j, $i;
  15.       my $c = () = $s =~ /$t/g;
  16.       if ($c > 1) {
  17.         $found = 1;
  18.         last;
  19.       }
  20.     }
  21.   }
  22.   return $s unless $found;
  23.   1 while $s =~ s/(?<=$t)(.*)$t/$1/;
  24.   return $s;
  25. }
  26. say delete_duplicate 'aeaAwww123abcdeあいうえおwww1235www123';
  27. say delete_duplicate 'abcdefabcde';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement