24
8
Your objective: Given a string of brackets, output the minimum Damerau-Levenshtein Distance required to turn the input string into a string where the brackets are balanced.
Input
The input string will only contain brackets and no other characters. That is, it is a combination of any of the characters in (){}[]<>. You may take input as either a string or an array of characters. You may not make any other assumptions about the input string; it may be arbitrarily long (up to the maximum size supported by your language), it may be empty, the brackets may already be balanced, etc.
Damerau-Levenshtein Distance
The Damerau-Levenshtein Distance between two strings is the minimum number of insertions, deletions, single-character substitutions, and transpositions (swapping) of two adjacent characters.
Output
The output should be the minimum Damerau-Levenshtein Distance between the input string and a string in which the brackets are matched. Output should be a number, not the resulting balanced string.
A pair of brackets is considered "matched" if the opening and closing brackets are in the right order and have no characters inside of them, such as
()
[]{}
Or if every sub-element inside of it is also matched.
[()()()()]
{<[]>}
(()())
Sub-elements can also be nested several layers deep.
[(){<><>[()]}<>()]
<[{((()))}]>
(Thanks to @DJMcMayhem for the definition)
Test Cases
Input Possible Balanced Output
Empty Empty 0
[](){}<> [](){}<> 0
[(){}<> [(){}<>] 1
[(]) []() 1
[[[[[[[[ [][][][] 4
(](<>}[>(}>><(>(({}] ()(<>)[(<><>){}] 7
>]{])< []{()} 3
([)}}>[ (){}<> 4
{<((<<][{{}>[<) <>(<<[]>{}>[]) 5
{><({((})>}}}{(}} {<><({()})>}{}{()} 4
(](<)>}[>(}>>{]<<(]] (<()<><<>()>>[])<()> 9
}})( {}() 2
(Thanks to @WheatWizard for solving half of the test cases)
This is code-golf, fewest bytes wins!
Your submissions should be testable, meaning it should output a result for each test case in no more than an hour.
9Balance your own brackets :P – Christopher – 2017-03-28T15:48:57.177
3I will be surprised if we'll even see a single correct non-bruteforce answer to this challenge. – orlp – 2017-03-28T16:08:15.313
Someone will just have to prove you wrong then :P – Pavel – 2017-03-28T16:10:23.270
There are too many corner cases. For example, We do not know how we should balance things like
[<>. – Matthew Roh – 2017-03-28T16:32:30.5375@SIGSEGV the answer to that is 1. It doesn't matter whether you balance it like
[<>]or[]<>or<>– Nathan Merrill – 2017-03-28T16:33:57.687@NathanMerrill Oh, I see. – Matthew Roh – 2017-03-28T16:34:51.010
This is hard, and though it's only been up for 4 hours I think it's telling that there are no answers yet. Can you maybe change it to be only ()[] rather than 4 different types of brackets? I think you get all the important propreties without making it into a significantly different challenge the way using only 1 type of brackets would. – Bijan – 2017-03-28T20:12:11.313
3@Bijan Nah, it won't make it much easier, and besides, Brain-Flak's birthday is coming up soon! – Pavel – 2017-03-28T20:19:09.063
1(I think) I have somehow solved it for addition purposes(imo, deletion is same number of additions here because of the symmetry). but i just cannot wrap my head around the editing thing.
I suggest a limit for the input string, because the problem seems computationally expensive (maybe NP-complete?) – qwr – 2017-03-30T20:46:23.827
3@qwr Why have a limit? The time limit only applies for the test cases given, for large inputs your program can take all the time in the world. – Pavel – 2017-03-30T20:56:57.147
2Error in the 7th test case: Consider
[]{()}(distance of 3) – math junkie – 2017-04-01T23:10:57.290@math_junkie thanks! I'm on mobile, can you make the edit yourself? – Pavel – 2017-04-02T02:18:07.443
i also edited in the 6 down to 5! – Roman Czyborra – 2017-04-02T18:25:14.930
@RomanCzyborra I don't think that actually works, can you comment a link to the steps you used? – math junkie – 2017-04-02T18:51:39.317
@math_junkie 0:{<((<<][{{}>[<) 1:<((<<][{{}>[<) 2:<>(<<][{{}>[<) 3:<>(<<[]{{}>[<) 4:<>(<<[]>{}>[<) 5:<>(<<[]>{}>[]) – Roman Czyborra – 2017-04-02T18:59:01.530
The second-to-last can be done in 9: – user1502040 – 2017-04-03T22:46:42.880
(<()<><<>()>>[])<()> <- (<()<><<>()>>[])<()] <- (<()<><<>()>>[])<(]] <- (<()<><<>()>>{])<(]] <- (<()<><<>(}>>{])<(]] <- (<()<><[>(}>>{])<(]] <- (<()<><[>(}>>{]<<(]] <- (]()<><[>(}>>{]<<(]] <- (]()<>}[>(}>>{]<<(]] <- (](<)>}[>(}>>{]<<(]]– user1502040 – 2017-04-03T22:46:59.030@user1502040 can you edit that in? – Pavel – 2017-04-04T05:07:00.517
Would using LISP be too obvious? – wooshinyobject – 2018-10-10T15:23:15.137