In this challenge you will be given an alphabetic string as input. We will define the "anti-string" of a given input to be the string with the case of all the letters inverted. For example
AaBbbUy -> aAbBBuY
You should write a program that takes a string as input and searches for the longest contiguous substring whose anti-string is also a contiguous substring. The two substrings should not overlap.
As an example if you were given the string
fAbbAcGfaBBagF
The bolded portions would be the longest string anti-string pair.
Your program should, once it has found the pair, collapse them into a single character each. It should do this by removing all but the first character of each substring. For example the string above
fAbbAcGfaBBagF
would become
fAcGfagF
Your program should then repeat the process until the longest string anti-string pair is a single character or shorter.
For example working with the same string the new longest pair after the collapse is
fAcGfagF
So we collapse the string again
fAcGag
Now the string cannot be collapsed further so we should output it.
In the case of a tie between candidate pairs (example AvaVA
) you may make either reduction (AaA
or AvV
, but not Aa
).
This is code-golf so answers will be scored in bytes with fewer bytes being better.
Test Cases
fAbbAcGfaBBagF -> fAcGag
AvaVA -> AaA / AvV
QQQQQQQ -> QQQQQQQ
fAbbAcQQQQaBBacqqqqA -> fAbcQBcq
gaq -> gaq
fAbbAcGfaBBagFaBBa -> fcGaBBag
Motivations
While this problem may seem arbitrary it is actually a problem I encountered while making code to process fundamental polygons. This process can be used to reduce a fundamental polygon to a smaller n-gon. After I tried it I thought it would make a nice little golf.
aaaAAAaaa -> aAaaa
? \$\endgroup\$