In this challence, your task is to locate substrings with a given structure.
Input
Your input shall be two non-empty alphanumeric strings, a pattern p
and a text t
.
The idea is that each character of p
represents a contiguous non-empty substring of t
which occur next to each other, and p
represents their concatenation.
Identical characters correspond to identical substrings; for example, the pattern aa
represents any non-empty square (a string obtained by concatenating a shorter string to itself).
Thus the pattern aa
can match the substring byebye
, with each a
matching bye
.
Output
If the text t
contains a substring that p
matches, then your output shall be that substring, with colons :
inserted between the strings that correspond to characters of p
.
For example, if we have t = byebyenow
and p = aa
, then bye:bye
is an acceptable output.
There may be several choices for the matching substring, but you shall only output one of them.
If t
does not contain a matching substring, your output shall be a sad face :(
.
Rules and Clarifications
Different characters of p
can correspond to identical substrings, so p = aba
can match the string AAA
.
Note that the characters must correspond to non-empty strings; in particular, if p
is longer than t
, the output must be :(
.
You can write a full program or a function, and you can also change the order of the two inputs. The lowest byte count wins, and standard loopholes are disallowed.
Test Cases
Given in the format pattern text -> output
.
Note that other acceptable outputs may exist.
a Not -> N
aa Not -> :(
abcd Not -> :(
aaa rerere -> re:re:re
xx ABAAAB -> A:A
MMM ABABBAABBAABBA -> ABBA:ABBA:ABBA
x33x 10100110011001 -> 10:1001:1001:10
abcacb 0a00cca0aa0cc0ca0aa0c00c0aaa0c -> c:a0aa:0c:c:0c:a0aa
abccab 0a00cca0aa0cc0ca0aa0c00c0aaa0c -> a:a:0c0:0c0:a:a
abcbcab 0a00cca0aa0cc0ca0aa0c00c0aaa0c -> :(
abcbdcab 0a00cca0aa0cc0ca0aa0c00c0aaa0c -> 00:c:ca0aa0c:c:0:ca0aa0c:00:c
O(2^((n * (n + 1))/2))
:P \$\endgroup\$