Remove letters while keeping strings unique

15

1

Inspired by this wonderful (based on the number of views and votes) challenge, which, in my humble opinion, has way too few answers.

Given (by any means) a list of strings, return (by any means) a set of letters that, when removed from the given strings, leaves the total length of (what remains of) the strings as small as possible, while keeping each string unique and at least one character long.

Examples:

Given "Day" and "day"; return "ay", because the given strings will be "D" and "d" when the characters "ay" are removed.

Given "Hello World!", "Hello world.", and "Hello world"; return "Helo Wrd" gives because the strings will be "!", "w.", and "w" when the characters "Helo Wrd" (with a space) are removed.

Given "century", "decade", "year", "month", "week", "day", "hour", "minute", and "second"; return "centurdowi" because the given words will be "y", "a", "ya", "mh", "k", "ay", "h", "m", "s" when the characters "centurdowi" are removed.

The order and format of the returned set is not important.

Adám

Posted 2015-12-21T18:36:13.953

Reputation: 37 779

1Your second case is wrong: "Helo Wrd" gives a total length of 4 with "!", "w." and "w". – Luke – 2015-12-21T20:26:29.050

1@Luke Thanks. I'll fix that. That shows that we need an algorithm, as doing it by hand is error prone. – Adám – 2015-12-21T20:31:19.400

And for the third one, 'centurdowi' yields 'y', 'a', 'ya', 'mh', 'k', 'ay', 'h', 'm', 's' for a total length of 12. – Luke – 2015-12-21T20:44:19.680

@Luke Thanks.

– Adám – 2015-12-21T20:54:59.700

+1 for using a challenge to help you in another challenge! – Luke – 2015-12-21T21:29:12.877

Test case: input ['CAB','FED'] output '' or "CAFE"? – edc65 – 2015-12-22T13:59:18.847

@edc65 'CAFE' or any equivalent ('ABED'...). OP: "keeping each string unique and at least one character long" – Adám – 2015-12-22T16:25:49.317

@nbz each string is unique and at least one character long from start in my example – edc65 – 2015-12-22T18:53:45.327

@edc65 "leaves the total length of (what remains of) the strings as small as possible," – Adám – 2015-12-23T02:15:11.113

Answers

4

Haskell, 138 130 bytes

import Data.List
c=concat
f i=snd$minimum[(length$c q,s)|s<-subsequences$nub$c i,q<-[map(filter(`notElem`s))i],nub q==q,all(>"")q]

Usage example: f ["century", "decade", "year", "month", "week", "day", "hour", "minute", "second"] -> "centurdoki".

This is a brute force approach.

     s<-subsequences$nub$c i  -- concatenate input i to a single string, remove
                              -- duplicates and make a list of all subsequences
       q<-[map(filter(...))i] -- remove chars appearing in subsequence s from all
                              -- input words, call result q
          nub q==q            -- keep those s where q has no duplicates (i.e. each
                              -- resulting string is unique) and
            all(>"")q         -- contains no empty strings
  (length$c q,s)              -- make pairs from all kept s, where the first element
                              -- is the combines length of all strings in q,
                              -- second element is s itself
snd$minimum                   -- find minimum of those pairs and discard length

Edit: @Seeq helped me saving 8 bytes. Thanks!

nimi

Posted 2015-12-21T18:36:13.953

Reputation: 34 639

How about map(#s), so you don't need to flip notElem? EDIT: Or couldn't you just inline it? – seequ – 2015-12-22T09:57:24.063

@Seeq: when call via map(#s), (#) must be defined as flip (filter . flip notElem). But of course inlining is far shorter. Thanks! – nimi – 2015-12-22T10:07:57.663

2

Pyth, 34

Takes input in the format ["century", "decade", "year", "month", "week", "day", "hour", "minute", "second"]. Golfing tips are appreciated, as always.

hh.mlsebfqlQl{eTf!}keTm,dm-kdQy{sQ

Luke

Posted 2015-12-21T18:36:13.953

Reputation: 5 091

2

Pyth, 24 bytes

hols-RNQf<}kJ-RTQ{IJy{sQ

Try it online. Test suite.

Note that the last test case will take a little while to run.

Takes input in array form, like ["Day", "day"].

Another interesting one I found and isaacg improved (also 24 bytes):

-J{sQhlDsM.A#f{ITm-RdQyJ

PurkkaKoodari

Posted 2015-12-21T18:36:13.953

Reputation: 16 699

I was able to reduce the second approach to 24 bytes: -J{sQhlDsM.A#f{ITm-RdQyJ here

– isaacg – 2015-12-22T09:10:11.360