Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Unique-Character Substring

by dchetlin (Friar)
on Jan 21, 2001 at 09:08 UTC ( #53291=note: print w/replies, xml ) Need Help??

in reply to Unique-Character Substring

Well, heck, if we're going to do this re-posting in a different medium thing, here's what I had on FWP. Without benchmarking, I put my money on it being faster.
#!/usr/bin/perl -w use strict; local $" = ','; while (<DATA>) { chomp; my @chars = split //; my $index = -1; my %seen = (); my $answer = [""]; while (++$index < @chars) { if (exists $seen{$chars[$index]}) { answer_set(\%seen, $answer); $index = $seen{$chars[$index]}; %seen = (); } else { $seen{$chars[$index]} = $index; } } answer_set(\%seen, $answer); print "$_: @$answer\n"; } sub answer_set { my $seen = shift; goto 'JUMP_' . ((keys %$seen <=> length $_[0][0]) + 1); JUMP_0: return; JUMP_2: $_[0] = []; JUMP_1: push @{$_[0]}, join "", sort { $seen->{$a} <=> $seen->{$b} } keys %$seen; return; } __DATA__ mississippi abcbef adbcbef that abcdefa abcdabc testing this test abcdefg


Replies are listed 'Best First'.
Re: Re: Unique-Character Substring
by salvadors (Pilgrim) on Jan 21, 2001 at 18:45 UTC

    Without benchmarking, I put my money on it being faster.

    I'd hold on to your money, if I were you. On a short string mine is about 4 times faster. On a longer string it's about 10 times faster...


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://53291]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-02-21 07:57 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (276 votes). Check out past polls.