One element of the list aa, ab, ba, bb, c has been removed, and the remainder shuffled and joined with no separator. Find the missing element.

For example, if the input was aaabbac, this is made by concatenating aa ab ba c, so the output would be bb.


  • Standard I/O rules apply.
  • You must take input as a string (or equivalent in your language), and you must use the characters abc as in the question; you cannot substitute them for others.
  • This is code golf, shortest code in bytes wins.

Test cases

cbbbaab  -> aa
bbbaabc  -> aa
cabbabb  -> aa
baaacbb  -> ab
bbcaaba  -> ab
caababb  -> ab
bbcabaa  -> ba
abcaabb  -> ba
abcbbaa  -> ba
bacaaab  -> bb
aacabba  -> bb
caaabba  -> bb
bbabaaba -> c
aaabbabb -> c
abaabbba -> c
  • \$\begingroup\$ So a list of characters isn't allowed if there is a string type? Just making sure \$\endgroup\$
    – noodle man
    Nov 7, 2023 at 13:01
  • \$\begingroup\$ Is printing "c " instead of "c" okay? \$\endgroup\$
    – Lynn
    Nov 7, 2023 at 22:08
  • \$\begingroup\$ @noodleman correct \$\endgroup\$
    – Sisyphus
    Nov 7, 2023 at 23:31
  • \$\begingroup\$ @Lynn I think I'll insist the output/return value should be exactly c \$\endgroup\$
    – Sisyphus
    Nov 7, 2023 at 23:32
  • 1
    \$\begingroup\$ Can we input the list of elements as well as the string to be tested \$\endgroup\$
    – Graham
    Nov 8, 2023 at 15:30

36 Answers 36


C# (without Implicit global usings), 241 bytes

var v=new System.Collections.Generic.List<string>();int i=0;while(v.Count<4){if(args[0][i]=='c')v.Add("c");else{v.Add(args[0].Substring(i,2));i++;}i++;}foreach(var k in"aa|ab|ba|bb|c".Split('|'))if(!v.Contains(k))System.Console.WriteLine(k);

This is my first golf, and the language isn't very golfable... be indulgent :-)

I didn't find a online compiler which handle input arguments correctly ; so to test this, paste it in a Visual Studio .cs file.

  • 1
    \$\begingroup\$ You can use tio to simulate the arguments like this. \$\endgroup\$
    – bsoelch
    Nov 9, 2023 at 9:26
  • 1
    \$\begingroup\$ You can save a few bytes when you replace the list with an array and a counter for the length and use ?=: instead of if-else tio, 189 bytes \$\endgroup\$
    – bsoelch
    Nov 9, 2023 at 9:43
  • \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ Nov 9, 2023 at 14:21
  • \$\begingroup\$ Building on @bsoelch 185 bytes \$\endgroup\$
    – ceilingcat
    Nov 10, 2023 at 18:44

APL+WIN, 69, 63, 57 bytes.

A bit more golfing saves another 6 bytes.

Prompts for string:


Try it online! Thanks to Dyalog Classic


Java (JDK), 118 bytes

Adaptation of Arnauld's first JS answer

(this could probably be outgolfed with the new base-16 trick)

s->{var r="";for(var e:("aaabbabbc"+s).split("(?<=\\G..|c)"))r=r.contains(e)?r.replace(e,""):r+e+" ";return r.trim();}

Try it online!

We concatenate the string of all possible values with the string parameter, so that the whole resulting string contains every value twice, except for the "missing" value which will only appear once.

Then we use a regex to split the whole string into either pairs of characters, or the character "c" alone.

We parse the resulting array, and in a string accumulator we add any unseen value (with a space separator) and remove every value already seen. At the end, the accumulator contains only the missing value, and lots of spaces that we trim.

  • \$\begingroup\$ Only test cases in which the inputs were mixed element by element and not by individual characters are relevant. But this (adopted) solution seems to work. \$\endgroup\$
    – Tobias321
    Nov 11, 2023 at 13:07
  • \$\begingroup\$ @Tobias321 I copied Arnauld's test cases. If i'm not mistaken, it contains only all possible permutations of elements, and not permutations of individual characters ? Or can you give me an example of such a case ? \$\endgroup\$
    – Fhuvi
    Nov 13, 2023 at 8:55
  • \$\begingroup\$ It was just a note. It's true, your test cases also contain all relevant test cases. \$\endgroup\$
    – Tobias321
    Nov 13, 2023 at 10:11
  • \$\begingroup\$ Example: All permutations of "aa", "ab", "ba", "bb" and "c" (missing one string) versus all permutations of "aaabbabbc" (missing one string). Only the former is sought (IIRC). \$\endgroup\$
    – Tobias321
    Nov 13, 2023 at 10:18

Gema, 96 65 characters


Sample run:

bash-5.2$ echo -n 'aaabbac' | gema 'c=@set{c;};??=@set{??;};\Z=${aa;aa}${ab;ab}${ba;ba}${bb;bb}${c;c}'

Try it online! / Try all test case online!


AWK, 123 120 bytes

a["aa"];a["ab"];a["ba"];a["bb"];{if(!sub(/c/,z))$0="c";else{for(i=1;i<6;i+=2)delete a[substr($0,i,2)];for(k in a)$0=k}}1

Try it online!

Ungolfed :

        for(i=1;i<6;i+=2) delete a[substr($0,i,2)]
        for(k in a)$0=k
  • 1
    \$\begingroup\$ No need for 3rd parameter in sub(/c/,z,$0) as defaults to $0. \$\endgroup\$
    – manatwork
    Dec 21, 2023 at 22:55

Swift, 187 bytes

Short version:

func m(_ s: String)->String{var t:Set=["aa","ab","ba","bb","c"];let s=s.map({String($0)});var i=0;while i<s.count{var x=s[i];if x != "c"{i+=1;x+=s[i];};t.remove(x);i+=1};return t.first!}

Verbose version:

func m2(_ s: String) -> String {
    var t:Set = ["aa","ab","ba","bb","c"]
    let s = s.map({String($0)})
    var i = 0
    while i < s.count {
        var x = s[i]
        if x != "c" {
            i += 1
            x += s[i]
        i += 1
    return t.first!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.