Inspired by this comment chain...
I wanna
enklactmy way out of this challenge but I can't...@ETHproductions to enklact (v): to implement a lookup table using a subsection consisting of unique elements.
Enklacting is a very useful way to compress a lookup table. For example, let's say you had the following list of colors:
red
green
blue
yellow
purple
orange
If you want to be able to take a color as input and return it's index in this list, there's obviously the straightforward way:
["red", "green", "blue", "yellow", "purple", "orange"].index(input())
But there's a way we could do this in way less bytes:
"rgbypo".index(input()[0])
This works because the first (or 0'th) index of each string is unique. This example is obvious, but sometimes it's a little bit harder. What if we wanted to make a lookup table for this list?
Sweet Onion Chicken Teriyaki
Oven Roasted Chicken
Turkey Breast
Italian BMT
Tuna
Black Forest Ham
Meatball Marinara
In this case, we can't do this:
"SOTITBM".index(input()[0])
because there are two different inputs that start with a 'T', namely "Tuna" and "Turkey". We must look at a different index. If you look at the 4th index of each string, you'll notice that they are all unique. So we can do this...
"enklact".index(input()[3])
In this case, the "enklaction string" is "enklact".
That leads us to today's challenge...
Given a list of strings, return any valid enklaction string. Or in other words, given a list of strings, return any new string where each letter is unique, and the string is formed by joining the i'th letter of each string.
If there is no valid enklaction string, your submission must return an empty string or a consistent falsy value instead. As usual, either functions or full programs are allowed, and the input/output formats are permissive (within reason).
Each string will only contain printable ASCII, and this challenge is case sensitive.
This is code-golf, so try to write the shortest program possible in your language of choice!
Test cases
Input:
Programming
Puzzles
Code
Golf
Output (any one of these):
"ozdl"
"gzef"
Input:
the quick
brown fox
jumped over
lazy dogs
Output:
"tbjl"
"hrua"
"eomz"
" wpy"
"qne "
"if o"
"kxvs"
Note that "u dd" and "coog" are not valid.
Input:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Output:
""
Input:
AbC
aBc
bbC
Output:
"Aab"
Input:
@#$%^_
Hello_World
How are you?
Output:
"#eo"
"$lw"
"%l "
"^oa"
Input:
a
ab
ac
Output:
""
-
\$\begingroup\$ Can we return a list of valid strings? \$\endgroup\$LyricLy– LyricLy2018年05月05日 02:25:46 +00:00Commented May 5, 2018 at 2:25
-
\$\begingroup\$ @LyricLy Hmm, now that I think about it, that would have made more sense. But since there are already answers and it isn't too much boilerplate to return the first one, I'm going to say no, it should be any one valid string. \$\endgroup\$DJMcMayhem– DJMcMayhem2018年05月05日 02:36:01 +00:00Commented May 5, 2018 at 2:36
-
\$\begingroup\$ Can we guarantee none of the inputted strings are empty? \$\endgroup\$musicman523– musicman5232018年05月05日 02:44:44 +00:00Commented May 5, 2018 at 2:44
-
8\$\begingroup\$ Can the consistent falsy value be an error of consistent type? \$\endgroup\$Stewie Griffin– Stewie Griffin2018年05月05日 08:18:14 +00:00Commented May 5, 2018 at 8:18
-
7\$\begingroup\$ Excuse me, but I think the correct verb is enklactate. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年05月05日 13:11:13 +00:00Commented May 5, 2018 at 13:11
34 Answers 34
Python 2, (削除) 68 (削除ここまで) (削除) 67 (削除ここまで) 61 bytes
lambda a:`{0:j for j in zip(*a)if len(set(j))==len(j)}`[6::5]
Improvements
If output would not have to be a string:
Python 3, 49 bytes
lambda a:[j for j in zip(*a)if len({*j})==len(j)]
-
-
\$\begingroup\$ @JoKing Elegant and implemented. \$\endgroup\$Neil– Neil2018年05月05日 05:37:01 +00:00Commented May 5, 2018 at 5:37
-
\$\begingroup\$ I think
max(`j`[2::5]for j in[""]+zip(*a)if len(set(j))==len(j))works, to save a byte. \$\endgroup\$lynn– lynn2018年05月05日 09:00:31 +00:00Commented May 5, 2018 at 9:00 -
\$\begingroup\$ @Lynn thanks and updated. \$\endgroup\$Neil– Neil2018年05月06日 07:52:22 +00:00Commented May 6, 2018 at 7:52
Python3, 59 bytes
lambda l:{len({*d}):''.join(d)for d in zip(*l)}.get(len(l))
Returns a string with the enklact, None otherwise
Retina, (削除) 43 (削除ここまで) 32 bytes
+/^(.).+^1円|^$/ms&m`^.
|""Lm`^.
Try it online! Edit: Saved 11 bytes thanks to @MartinEnder. Explanation:
+
Repeat while the input changes...
/^(.).+^1円|^$/ms&
...only if one line is empty or two lines begin with the same character...
m`^.
...delete the first character of every line. The repeat therefore stops if either a) all the lines begin with different characters, in which case the condition fails and the input does not get changed, or b) at least one line becomes empty, in which case all the characters eventually get deleted, at which point the input stops changing.
|""L`^.
Collect the first character of every line. (If there was no solution, the above loop will have deleted everything and there will be nothing to collect.)
-
\$\begingroup\$ Regex options also take regex modifiers (by writing them directly after the closing delimiter): tio.run/##K0otycxLNPz/… \$\endgroup\$Martin Ender– Martin Ender2018年05月06日 07:33:52 +00:00Commented May 6, 2018 at 7:33
-
\$\begingroup\$ Actually, that lets you get rid of the second stage entirely: tio.run/##K0otycxLNPz/X1s/… (somehow can't apply
mwith a group here, it looks like the conditional stage doesn't propagate the option). \$\endgroup\$Martin Ender– Martin Ender2018年05月06日 07:42:39 +00:00Commented May 6, 2018 at 7:42 -
\$\begingroup\$ Ah, of course, loop a conditional instead of a conditional loop, that solves the infinite loop bug in your first version. Very neat! \$\endgroup\$Neil– Neil2018年05月06日 09:35:37 +00:00Commented May 6, 2018 at 9:35
Haskell, 71 bytes
f x|elem""x=""|y<-head<$>x,and[filter(==a)y==[a]|a<-y]=y|1<2=f$tail<$>x
BMO saved 3 bytes with any null x → elem""x.
Ørjan Johansen saved a byte with sum[1|b<-y,a==b]<2 → filter(==a)[y]==[a].
Explanation
f x|elem""x="" -- Once any of the strings is empty, return "".
|y<-head<$>x -- Otherwise, let y be all the first letters...
,and[ |a<-y] -- If, for all a in y,
filter(==a)y==[a] -- a occurs just once in y:
=y -- return y.
|1<2=f$tail<$>x -- Else, chop off all the first letters and recurse.
If throwing an error (Prelude.head: empty list) when there's no solution is OK, |elem""x="" can be erased for 61 bytes.
-
1\$\begingroup\$ Shorter test:
filter(==a)y==[a]\$\endgroup\$Ørjan Johansen– Ørjan Johansen2018年05月05日 16:13:49 +00:00Commented May 5, 2018 at 16:13
-
\$\begingroup\$ Fails if there is no match and the first string is not the shortest. \$\endgroup\$G B– G B2018年05月07日 12:32:40 +00:00Commented May 7, 2018 at 12:32
-
\$\begingroup\$ @GB Could you provide an example, please? I modified my last test according to your description, and it worked. \$\endgroup\$Kirill L.– Kirill L.2018年05月07日 13:22:26 +00:00Commented May 7, 2018 at 13:22
-
\$\begingroup\$ Try ["abc", "ac", "acd"] \$\endgroup\$G B– G B2018年05月08日 11:09:46 +00:00Commented May 8, 2018 at 11:09
-
\$\begingroup\$ Now I see, you're right. Should be fixed. \$\endgroup\$Kirill L.– Kirill L.2018年05月08日 11:36:54 +00:00Commented May 8, 2018 at 11:36
Pyth, 6 bytes
>1{I#C
The output is a singleton list, as allowed by default; the list [ ] (empty list, falsy) is returned in case the string cannot be enklactified.
Explanation
>1{I#C – Full program.
C – Transpose the input, cropping absences.
# – Filter by:
{I – Invariant under deduplication.>1 – Slice to 1. list[:1] in Python.
Pyth, 5 bytes
This would've been valid if crashing would have counted as a falsy value.
h{I#C
Jelly, 7 bytes
z0Q€fZḢ
Returns the integer 0 if the strings cannot be enklactified.
How it works
z0Q€fZḢ Main link. Argument: A (string array)
z0 Zip/transpose, filling shorter rows with 0.
Q€ Unique each deduplicate resulting string.
Z Zip/transpose, without using a filler.
f Filter; keep only string that appear in the results to both sides.
Ḣ Head; extract the first string. Returns 0 if the array is empty.
R, (削除) 116 (削除ここまで) (削除) 107 (削除ここまで) 95 bytes
R+pryr
pryr::f(for(i in 1:min(nchar(v)))`if`(anyDuplicated(t<-substr(v,i,i)),0,{cat(t,sep="")
break}))
base R
function(v)for(i in 1:min(nchar(v)))`if`(anyDuplicated(t<-substr(v,i,i)),0,{cat(t,sep="")
v=0})
These two variants saving 9 bytes thanks to Giuseppe.
Explanation:
This effectively truncates all stings in the vector v to the length of the shortest and iterates through subsequent indexes. Then checks whether there are any duplicates within chosen letters and if not, pastes them together and prints with cat. If all indexes return duplicated results, this prints empty string.
All wraped in an anonymous pryr function with break to halt the loop, or base R function zeroing the vector to break the loop.
-
1
Haskell, (削除) 76 (削除ここまで) 74 bytes
f t=last$"":(id=<<foldr(zipWith(#))([[]]<$t)t)
x#[s]=[x:s|all(/=x)s]
x#e=e
Try it online! Returns the last valid lookup string or an empty string if no such string exists.
(削除) 71 (削除ここまで) 69 bytes
If throwing a consistent exception as falsy value is allowed:
f t=head$id=<<foldr(zipWith(#))([[]]<$t)t
x#[s]=[x:s|all(/=x)s]
x#e=e
Try it online! Throws an empty list exception if no string is found, returns the first valid string otherwise.
-2 bytes thanks to Ørjan Johansen
-
1\$\begingroup\$
notElem xcan be shortened toall(/=x). \$\endgroup\$Ørjan Johansen– Ørjan Johansen2018年05月05日 16:04:52 +00:00Commented May 5, 2018 at 16:04
Stax, (削除) 9 (削除ここまで) 8 bytes
åτIns↓μg
Explanation (unpacked):
M{c0-u=}j Full program, implicit input
e.g. ["Programming", "Puzzles", "Code", "Golf"]
M Transpose
["PPCG", "ruoo", "ozdl", "gzef", "rl0円0円", "ae0円0円", "ms0円0円", "m0円0円0円", "i0円0円0円", "n0円0円0円", "g0円0円0円"]
{ }j Find first matching element:
e.g. "PPCG"
c0- Copy and remove zero bytes (padding)
"PPCG" "PPCG"
u Unique
"PPCG" "PCG"
= Check if equal:
1
First matching here: "ozdl". If none is found, the stack is empty
Implicit output if anything on stack
R, 127 bytes
function(S,s=sapply(S,substring,x<-1:max(nchar(S)+1),x))cat(rbind(s[!apply(s,1,anyDuplicated)&!rowSums(s==""),],"")[1,],sep="")
sapply typically returns a matrix when all the length(FUN(X[[i]])) are equal, except when length(FUN(X[[i]]))==1, in which case it returns a vector. In order to use the matrix operations, then, we have to substring one further than we need to guarantee a matrix, which is why x extends to max(nchar(S)+1).
We then filter for rows that have no duplicates and no empty strings. Since we can only return a single string, we take the first one, except that when there are no rows that meet the criteria, we would throw an error, so we append an extra row of "" to the end.
Then we print out the enklactified string or the empty string.
05AB1E, 7 bytes
øʒDÙQ}н
Explanation
ø # Zip the input array
ʒ } # Filter by ...
DÙQ # ... an entry is equal to itself deduplicated
н # Take the first element
Perl 6, 27 bytes
{([Z] $_).first:{.Set==$_}}
After seeing that the ruby version has been upvoted, I have copied the approach and used character lists, instead of strings. I don't like it.
My older, and more correct submission is as below:
Perl 6, 38 bytes
Split, zip, check uniqueness, join.
{[~] ([Z] @_>>.comb).first:{.Set==$_}}
Perl 5 -plF, 56 bytes
Saved 5 bytes thanks to @Xcali!
map$;[$_].=$F[$_],0..@F}{($_)=grep$.==y///c*!/(.).*1円/,@
-
1\$\begingroup\$ Using
$.instead of$-can save 5 bytes: Try it online! \$\endgroup\$Xcali– Xcali2020年11月21日 19:03:08 +00:00Commented Nov 21, 2020 at 19:03 -
\$\begingroup\$ @Xcali of course! That's great, thank you! \$\endgroup\$Dom Hastings– Dom Hastings2020年11月21日 19:12:22 +00:00Commented Nov 21, 2020 at 19:12
Japt v2.0a0, 7 bytes
Takes input as an array of (削除) character arrays (削除ここまで) strings, returns (削除) an array of characters (削除ここまで) a string or undefined.
ÕæÈ¶ ̧¬â
-
\$\begingroup\$ I tried the challenge out before reading your solution and got a nearly identical solution:
z æ_¬eZ¬â\$\endgroup\$Etheryte– Etheryte2018年05月05日 09:16:16 +00:00Commented May 5, 2018 at 9:16 -
\$\begingroup\$ Exactly identical, other than the input format. \$\endgroup\$Shaggy– Shaggy2018年05月06日 21:13:46 +00:00Commented May 6, 2018 at 21:13
Python 3, 75 bytes
def f(t):c=[s.pop(0)for s in t];return all(t)and(f(t),c)[len(t)==len({*c})]
Operates on character lists instead of strings. Returns False if no valid enklaction string exists.
-
\$\begingroup\$ I think this recurses, which would make it invalid unless the
f=is included in the byte count. \$\endgroup\$LyricLy– LyricLy2018年05月05日 03:01:49 +00:00Commented May 5, 2018 at 3:01 -
\$\begingroup\$ @LyricLy Fixed :) \$\endgroup\$musicman523– musicman5232018年05月05日 03:15:14 +00:00Commented May 5, 2018 at 3:15
C (gcc), 161 bytes
f(s,i)char**s;{char**t,a[255],*u=a;for(i=0;memset(a,0,255),u&&~i;i+=!!~i&&u)for(t=s;(u=u?*t++:0)&&~(i=u[i]?i:-1)&&!a[u[i]]++;);while(~i&&(u=*s++))putchar(u[i]);}
Each character position is tested for duplication and skipped if a duplicate is detected; this is continued until the shortest string ends. It's ASCII only, sadly: DBCS/UTF-8 strings break this function badly!
-
\$\begingroup\$ 152 bytes \$\endgroup\$ceilingcat– ceilingcat2019年01月01日 00:23:28 +00:00Commented Jan 1, 2019 at 0:23
Japt, 12 bytes
Returns undefined for non-enklactic strings.
y ·æ_¬n ä¦ e
Explanation:
y ·æ_¬n ä¦ e
y // Split the input at newlines and transpose
· // Join on newlines
æ_ // Return the first item that returns truthy when ran through:
¬n // Sort
ä¦ // Reduce with !=
e // All items are truthy (unique)
-
\$\begingroup\$ You should be able to save 2 bytes by taking input as an array of character arrays and ditching the two
splits. \$\endgroup\$Shaggy– Shaggy2018年05月05日 12:07:33 +00:00Commented May 5, 2018 at 12:07
Wolfram Language (Mathematica), 54 bytes
#&@@Select[PadRight@#,#~FreeQ~0&&Union@#==Sort@#&]&
Takes a list of list of characters as input, returns a list of characters. Contains U+F3C7, corresponding to "Transpose" operator.
Returns #1 and throws a bunch of ignorable errors when there is no suitable string.
Explanation:
PadRight@#
Pad the input so that each "string" (list of characters) is the same length. This appends integer 0s (not string "0"s). Then transpose.
Select[ ... ,#~FreeQ~0&&Union@#==Sort@#&]
Select the strings that have no integer 0s in them and have all unique characters.
#&@@
Get the first one.
JavaScript (ES6), 66 bytes
Returns either a string or undefined if no solution exists.
f=(a,i=0)=>a.every(s=>(o[k+=c=s[i],c]^=1)&&c,o=k=[])?k:c&&f(a,i+1)
Commented
f = ( // f = recursive function taking:
a, // a[] = input array
i = 0 // i = pointer
) => //
a.every(s => // for each string s in a[]:
(o[k += // append to the key string k
c = s[i], // the character c at position i in s
c] ^= 1 // and toggle o[c] (undefined -> 1 -> 0)
) && c, // additionally, make sure that c is defined
o = k = [] // start with o = k = empty array
) ? // every() is true if all characters were distinct and defined:
k // in which case we return k
: // else:
c && // provided that every() didn't fail because of an undefined character,
f(a, i + 1) // try again at the next position
-
\$\begingroup\$ Huh, when did a)
qstop always being a string b) StringMap start working on non-strings? Anyway, yourRangeis unnecessary, saving you 2 bytes. \$\endgroup\$Neil– Neil2018年05月05日 10:11:22 +00:00Commented May 5, 2018 at 10:11 -
\$\begingroup\$ @Neil a) When I added array/object input b) >_> I'm not sure. probably around the time when I fixed the while loop (sorry, forgot to mention either of those) \$\endgroup\$ASCII-only– ASCII-only2018年05月05日 23:34:10 +00:00Commented May 5, 2018 at 23:34
Husk, 9 bytes
ḟS=UḞz:∞ø
Explanation
fS=UḞz:∞ø
Ḟz:∞ø Transpose the input dropping characters of longer strings
Ḟ Fold right
z: Zip with prepend
∞ø Infinite list of empty lists
ḟS=U Find the first string without duplicates, returns an empty string if none
ḟ Return first value satisfying predicate
= Equal
S U to itself with duplicates removed
-
\$\begingroup\$ Using
←ġLToverḞz:∞øshould save a byte. \$\endgroup\$ბიმო– ბიმო2018年05月17日 23:41:28 +00:00Commented May 17, 2018 at 23:41
Retina, (削除) 81 (削除ここまで) 56 bytes
m`$
$.=*£¢
N$`.
$.%`
¶
~`(.*?¢)+
L`.{$#1}
A`(.).*1円|£|¢
-25 bytes thanks to @Neil
Transposing rectangular text in Retina is surprisingly difficult.
-
\$\begingroup\$ I wish I knew a better way of performing rectangular transposition, but in the mean time, save 25 bytes. \$\endgroup\$Neil– Neil2018年05月07日 11:07:03 +00:00Commented May 7, 2018 at 11:07
-
\$\begingroup\$ @Neil Ahh... eval stage. I always forget retina 1.0 has all these cool new features \$\endgroup\$TwiN– TwiN2018年05月07日 11:16:09 +00:00Commented May 7, 2018 at 11:16
C (gcc), (削除) 121 (削除ここまで) (削除) 113 (削除ここまで) 110 bytes
i;d;f(s)char**s;{char**_=s,x[255]={0},y[99]={0};for(d=i=0;*_;)d+=x[y[i++]=*(*_++)++]++;d=d?*x?0:f(s):puts(y);}
Ungolfed
void enklactify(char *strings[]) {
int quit = 0;
while (!quit) {
char **arg = strings; // current row
int exists[255] = {0}; // which characters exist in the column
char output[99] = {0}; // will hold output string
int index = 0; // where to insert in output
int duplicates = 0; // have we found any duplicates?
while (*arg != NULL) {
char *word = *arg; // current word
char first = *word; // first letter of current word
if (exists[first])
duplicates = 1; // we found a duplicate
exists[first] = 1; // mark it as in our string
output[index] = first; // add it to our output
index++;
(*arg)++; // advances the current word to the next
// character by reference (chops first char)
arg++; // advance to next whole argument
}
if (!duplicates) { // This is a valid solution
puts(output);
quit = 1;
}
if (exists[0]) { // We hit the null terminator of one of the
// input strings, so we failed
quit = 1;
}
}
}
-3 thanks to ceilingcat
This abuses the ability to just add 1 to a string pointer in C to get the 'tail' of the string. The main byte savers are:
d+=x[y[i++]=*(*_)++]++which appends the first character of the first string of_toy, advances the first string of_to remove its first character, adds thexentry at that character tod, and increments saidxentryq=d?*x:puts(y)which printsyifdis non-zero while settingqto a non-zero value, or setsqto non-zero if the first element ofxis non-zero (if we were at the end of one of the strings then said element would be non-zero)
Edit: Shaved bytes by switching from while loop to recursive tail call and removing brackets from for loop.
-
\$\begingroup\$ Suggest
for(d=i=0;*_;)instead offor(d=0,i=0;*_;++_)and*(*_++)++]++;d=d?!*x*f(s)instead of*(*_)++]++;d=d?*x?0:f(s)\$\endgroup\$ceilingcat– ceilingcat2018年06月21日 22:14:48 +00:00Commented Jun 21, 2018 at 22:14
Husk, 8 bytes
ḟȯ=L1LUT
Different approach to the previous Husk answer. Gets the longest prefix with all-unique characters from each index position, and then selects the first that is the same length as the number of input strings.
ḟ # first element that satisfies
ȯ=L1L # length (of string) equals length of input (array), of
U # longest prefixes of all unique characters of
T # transpose of input
-
\$\begingroup\$ Looks like the other answer is also 8 bytes using the golf in the comments. \$\endgroup\$Razetime– Razetime2020年11月22日 03:50:38 +00:00Commented Nov 22, 2020 at 3:50
-
1\$\begingroup\$ @Razetime - yes, I noticed that, too. So I don't claim to be better, just different... \$\endgroup\$Dominic van Essen– Dominic van Essen2020年11月22日 08:11:25 +00:00Commented Nov 22, 2020 at 8:11
-
\$\begingroup\$
ḟS=uTwould be shorter, no? \$\endgroup\$Razetime– Razetime2021年04月08日 16:21:10 +00:00Commented Apr 8, 2021 at 16:21 -
1\$\begingroup\$ @Razetime - unfortunately, that gives output for the test cases that shouldn't have a solution... \$\endgroup\$Dominic van Essen– Dominic van Essen2021年04月09日 07:11:31 +00:00Commented Apr 9, 2021 at 7:11
Pyth, 13 bytes
e+kf{[email protected]
e+kf{[email protected]
.tQ Transpose the (implicit) input with padding.
.TQ Transpose the input without padding.
@ Take the strings in both.
f{IT Find the ones that have no duplicates.
e+k Get the last, or an empty string.
-
1\$\begingroup\$ It seems that returning a list of all valid strings is not valid. \$\endgroup\$LyricLy– LyricLy2018年05月05日 02:40:00 +00:00Commented May 5, 2018 at 2:40
-
\$\begingroup\$ @LyricLy Fixed. \$\endgroup\$user48543– user485432018年05月05日 02:40:30 +00:00Commented May 5, 2018 at 2:40
Red, 139 bytes
func[b][m: length? first b foreach a b[m: min m length? a]repeat n m[c: copy[]foreach a b[append c a/(n)]if c = unique c[return rejoin c]]]
Explanation:
Takes the input as a block (list) of strings. Returns the enklaction string or none otherwise.
f: func[b][
m: length? first b ; the minimal length of the first string
foreach a b[m: min m length? a] ; find the minimal length of all strings
repeat n m[ ; limit the search to the minimal length
c: copy[] ; an empty block
foreach a b[append c a/(n)] ; for each string append the current char
if c = unique c[return rejoin c] ; if all chars are unique, return the block
]
]
Röda, (削除) 80 (削除ここまで) 77 bytes
f a{a|seq 0,#_|try{{|i|a|[_[i:i+1]]|orderedUniq|concat|[_]if[#_1=#a]}_|head}}
-1 byte thanks to Cows quack
Explanation:
f a{
a| /* Push the strings in a to the stream */
/* For each string (_): */
seq 0,#_| /* Push a range from 0 to the length of _ to the stream */
try{ /* Ignore errors during the following block */
{|i| /* For each i in the stream: */
a| /* Push strings in a to the stream */
[_[i:i+1]]| /* For each string, push the ith character to the stream */
orderedUniq| /* Remove duplicate characters */
concat| /* Join the characters into a string */
[_]if /* Push the string to the stream if */
[#_1=#a] /* Its length is the length of a */
}_|
head /* Take the first string in the stream and return it */
}
}
The try keyword is used to dismiss errors that occur if i is larger than the length of the smallest string in a, or there is no answer and head causes an error.
-
\$\begingroup\$ You can remove the parens in
seqto save a byte \$\endgroup\$user41805– user418052018年05月05日 20:31:54 +00:00Commented May 5, 2018 at 20:31
Java 10, 106 bytes
a->{for(int i=0;;i++){var r="";for(var s:a)r+=s[i];if(r.length()==r.chars().distinct().count())return r;}}
Will give an error instead of returning an empty String if no solution can be found. Input is a character-matrix.
Explanation:
a->{ // Method with character-matrix parameter and String return-type
for(int i=0;;i++){ // Loop `i` upwards
var r=""; // Result-String, starting empty
for(var s:a) // Loop over the character-arrays of the input
r+=s[i]; // And append every `i`'th character to `r`
if(r.length()==r.chars().distinct().count())
// If `r` only contains unique characters
return r;}} // Return `r` as result
-
\$\begingroup\$ OP didn't seem to want to allow errors, although admittedly never put it in the post itself. \$\endgroup\$Ørjan Johansen– Ørjan Johansen2018年05月07日 15:58:16 +00:00Commented May 7, 2018 at 15:58
Clojure, 59 bytes
#(for[s(apply map list %):when(=(count(set s))(count %))]s)
Returns a list of lists of characters.