14
\$\begingroup\$

Given one line that consists of only letters, process as following:

  • You maintain a string that's empty at the beginning.
  • If the next input character is in the string, remove it from the string.
  • If the next input character isn't in the string, append it to the string.

Output the final state of the string.

You can safely assume the input consists at least one character (i.e. non-empty), but there's no guarantee that the output isn't empty.

Pseudocode (Feel free to golf this):

str = EMPTY
for each character ch in input
  if ch exists in str
    remove all ch from str
  else
    append ch to str
print str

The input matches the regular expression ^[A-Za-z]+$.

Sample test cases:

ABCDBCCBE -> ADCBE
ABCXYZCABXAYZ -> A
aAABBbAbbB -> aAbB
GG -> (empty)

The input can be given in any applicable way, but it must be treated as a string, and the same for output. The program should not exit with an error.

The shortest program in each language wins!

Extra (Optional): Please explain how your program works. Thank you.

\$\endgroup\$
  • \$\begingroup\$ May the line be empty? \$\endgroup\$ – user202729 Nov 28 '17 at 15:37
  • 1
    \$\begingroup\$ @user202729 No. I changed a little (it does not invalidate any answer) so the input is never empty. \$\endgroup\$ – iBug Nov 28 '17 at 16:03
  • 1
    \$\begingroup\$ So why did you reject ais523's edit suggestion (link)? \$\endgroup\$ – user202729 Nov 28 '17 at 16:09

32 Answers 32

0
\$\begingroup\$

Julia 0.6, 49 bytes

~x=foldl((s,c)->in(c,s)?replace(s,c,""):s*c,"",x)

Try it online!

Algorithm from the pseudocode. This does not work like this in tio.run because concatenation of a string with a char using * is not defined. It does work in JuliaPro 0.6.1.1 however, where this is defined from the future™ (0.7 dev).

\$\endgroup\$
0
\$\begingroup\$

Japt -h, 15 bytes

;£A=øX ?AkX:ApX

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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