Surrounded and reversed

In this challenge, your task is to determine whether some string occurs as a substring of a given string both surrounded by another string and reversed.

Your input is a non-empty string S of lowercase ASCII letters. If there exist non-empty strings A and B such that the concatenation ABA and the reversal rev(B) are both contiguous substrings of S, then your output shall be a truthy value. Otherwise, your output shall be a falsy value. The two substrings may occur in any order, and they can even overlap. However, it can be shown that in all truthy cases, you can either find non-overlapping substrings or a solution where B is a palindrome.

Example

Consider the input string S = zyxuxyzxuyx. We choose A = xu and B = xyz, and then we can find the following substrings:

S      = zyxuxyzxuyx
ABA    =   xuxyzxu
rev(B) = zyx


The choice A = x and B = u would also be valid. The correct output in this case is truthy.

Rules and scoring

You can write a full program or a function. Standard loopholes are disallowed. The lowest byte count wins.

Test cases

Truthy

sds
aaxyxaa
aaxyaaayx
hgygjgyygj
zyxuxyzxuyx
cgagciicgxcciac
iecgiieagcigaci
oilonnsilasionloiaammialn
abbccdeeaabccddbaacdbbaccdbaaeeccddb


Falsy

a
aaxyaa
jjygjhhyghj
abcaabbccaabca
egaacxiagxcaigx
lnsiiosilnmosaioollnoailm


Fun fact

One can construct arbitrarily long falsy strings using four distinct letters, but not using three letters. The fourth falsy test case is the longest possible.

• I think abcaabbccaabcaa is still falsy with three distinct letters. Jun 20, 2017 at 12:06
• @EriktheOutgolfer Choose A = caa and B = b, and look at the end of the string. Jun 20, 2017 at 12:09
• Oh...yeah I don't have much time to check while golfing... Jun 20, 2017 at 12:13

Brachylog, 13 bytes

s↔B&sA,B,A~s?


Try it online!

How it works

s↔B&sA,B,A~s?
s              the input has substring temp
↔B            temp reversed is B
&           and the input
sA         has substring A
,B,A     A,B,A
~s?  is a substring of input

• Ah right. I was wondering how you ensured that A was nonempty, but the s immediately after the & takes care of that.
– user62131
Jun 20, 2017 at 12:52

Retina, 40 bytes

(.+)(.)+\1.*(?<=(?=(?<-2>\2)+(?(2)A)).*)


Try it online!

Big thanks to Martin for fixing a bug with the expression.

Matches ABA then the rest of the string. While doing this, it pushes B's characters onto a stack. Looks backwards over the whole string to find B in reverse order, by traversing forward again and popping characters off of the stack as it matches them ((?<-2>\2)+). Then it makes sure the stack is empty by trying to match a capital letter, which won't be in the input, if the stack still is full.

Ruby, 53 bytes

->s{s.scan(/(?=(.+)(.+)\1)/).any?{|a,b|s[b.reverse]}}


Fairly straightforward. The only real trick is the positive lookahead (?=...) in the regex, which is used to find all matches and not just non-overlapping ones.

Try it online!

PHP, 95 bytes

<?=($p=preg_match_all)('#(?=(.+)(.+)\1)#',$argn,$t)&&$p(strrev("#".join("|",$t[2])."#"),$argn);


Try it online!

Jelly, 21 bytes

Ḣ⁼Ṫȧ
ẆŒṖ€ẎÇÐfẎ€QUẇ€⁸Ṁ


Try it online!

Very inefficient.