27
1
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.
If the largest substring with anti-string substrings has more than one anit-string substring, should all substrings be collapsed or only the first two? – Jonathan Frech – 2018-02-21T23:29:49.747
@JonathanFrech Any two. That is a case where there is a tie between candidate pairs. – Post Rock Garf Hunter – 2018-02-21T23:31:07.123
So
aaaAAAaaa -> aAaaa
? – Jonathan Frech – 2018-02-21T23:35:23.107Something about a subset of this problem screams quine but I can't put my finger on it. – Magic Octopus Urn – 2018-02-22T00:57:59.943
1@MagicOctopusUrn Something like Write a two-cycle quine where the program's output is its antistring? – Jonathan Frech – 2018-02-22T01:24:04.800
The case inversion is according to which locale? What is your input character set? – Lightness Races with Monica – 2018-02-22T01:34:02.293
How should the case 'aAa' be resolved? It contains substrings 'aA' and 'Aa' which are antistrings; is the result then 'aA'? or must the strings be non-overlapping? – Chas Brown – 2018-02-22T08:00:42.560
I came here hoping that "antistring" meant something that destroys a string (like antimatter). Inverted case is not near as satisfying. – NH. – 2018-02-22T17:55:11.283
@ChasBrown I covered an example like that. The string and it's antistring must be non-overlapping. – Post Rock Garf Hunter – 2018-02-22T19:01:08.523