Skip to main content
one byte shorter, along with another equal length solution
Source Link
DanTheMan
  • 4.1k
  • 16
  • 41

Wolfram Language, 101101 100 bytes

StringJoin[Characters@#/.(r={"L"->-"R","U"->-"D"})//.{a___,x__,b___}/;Tr[{x}]==0->{a,b}/.Reverse/@r]&
StringJoin[Characters@#/.(r={"L"->-"R","U"->-"D"})//.{a___,x__,b___}/;Tr@{x}==0->{a,b}/.Reverse/@r]&

Try it online!Try it online!

With some fancier formatting and comments:

StringJoin[                                       (*reconvert to input format*)
  Characters@#                                    (*split into characters*)
  /. (r = {"L" -> -"R", "U" -> -"D"})             (*map L to -R and U to -D*)
  //. {a___, x__, b___} /; Tr[Tr@{x}] == 0 -> {a, b}  (*delete runs that sum to 0*)
  /. Reverse /@ r                                 (*convert -R and -D back to L and U*)
]&

This takes a similar method to some of the others, deleting runs that sum to zero, but this one does it by replacing L and U with negative R and negative D respectively.

Another 100 byte solution that uses a similar but distinct technique, using SequenceReplace:

StringJoin@SequenceReplace[Characters@#/.(r={"L"->-"R","U"->-"D"}),{__}?(Tr@#==0&)->{}]/.Reverse/@r&

Try it online!

Wolfram Language, 101 bytes

StringJoin[Characters@#/.(r={"L"->-"R","U"->-"D"})//.{a___,x__,b___}/;Tr[{x}]==0->{a,b}/.Reverse/@r]&

Try it online!

With some fancier formatting and comments:

StringJoin[                                       (*reconvert to input format*)
  Characters@#                                    (*split into characters*)
  /. (r = {"L" -> -"R", "U" -> -"D"})             (*map L to -R and U to -D*)
  //. {a___, x__, b___} /; Tr[{x}] == 0 -> {a, b} (*delete runs that sum to 0*)
  /. Reverse /@ r                                 (*convert -R and -D back to L and U*)
]&

This takes a similar method to some of the others, deleting runs that sum to zero, but this one does it by replacing L and U with negative R and negative D respectively.

Wolfram Language, 101 100 bytes

StringJoin[Characters@#/.(r={"L"->-"R","U"->-"D"})//.{a___,x__,b___}/;Tr@{x}==0->{a,b}/.Reverse/@r]&

Try it online!

With some fancier formatting and comments:

StringJoin[                                       (*reconvert to input format*)
  Characters@#                                    (*split into characters*)
  /. (r = {"L" -> -"R", "U" -> -"D"})             (*map L to -R and U to -D*)
  //. {a___, x__, b___} /; Tr@{x} == 0 -> {a, b}  (*delete runs that sum to 0*)
  /. Reverse /@ r                                 (*convert -R and -D back to L and U*)
]&

This takes a similar method to some of the others, deleting runs that sum to zero, but this one does it by replacing L and U with negative R and negative D respectively.

Another 100 byte solution that uses a similar but distinct technique, using SequenceReplace:

StringJoin@SequenceReplace[Characters@#/.(r={"L"->-"R","U"->-"D"}),{__}?(Tr@#==0&)->{}]/.Reverse/@r&

Try it online!

Source Link
DanTheMan
  • 4.1k
  • 16
  • 41

Wolfram Language, 101 bytes

StringJoin[Characters@#/.(r={"L"->-"R","U"->-"D"})//.{a___,x__,b___}/;Tr[{x}]==0->{a,b}/.Reverse/@r]&

Try it online!

With some fancier formatting and comments:

StringJoin[                                       (*reconvert to input format*)
  Characters@#                                    (*split into characters*)
  /. (r = {"L" -> -"R", "U" -> -"D"})             (*map L to -R and U to -D*)
  //. {a___, x__, b___} /; Tr[{x}] == 0 -> {a, b} (*delete runs that sum to 0*)
  /. Reverse /@ r                                 (*convert -R and -D back to L and U*)
]&

This takes a similar method to some of the others, deleting runs that sum to zero, but this one does it by replacing L and U with negative R and negative D respectively.