Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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


Comment on Re: Unique-Character Substring
Download Code
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2016-05-26 08:14 GMT
Find Nodes?
    Voting Booth?