How i'd handle this one :
Let's declare 2 variables :
int factorials_quotient = 1;
int solution_count = 0;
As an example, il will use string : abc cme eqk aa a bb b
, since it it the trickiest case
FIRST : identify the 'concat' strings.
Exemple : abc cme eqk aa a bb b
Since 'abc cme eqk
' string only have a unique order, you MUST concatenate them and consider it as a unique string : abccmeeqk aa a bb b
. (For each concatenation, if the first letter equals the last letter on the substring, you got a exeption case, you must handdle it).
SECOND: identify the 'factorials' strings.
Exemple : abccmeeqk aa a bb b
We are looking for two or more string containing the same characters like : aa a
& bb b
(DO NOT count 'abcmeeqk' since it contains char different from a).
For each of these string multiply factorials_quotient
by n!
In our case, we first got aa a
, since we have 2 string (a and aa) we multiply factorials_quotient
by factorial(2), then the same for bb b
. our factorials_quotient
now equals 1 * 2! * 2! = 4.
Then concatenate aa
to a
and bb
to b
.
We now got : abccmeeqk aaa bbb
.
THIRD : try to concatenante the factorials and the concats if it is possible.
Exemple we not have abccmeeqk aaa bbb
.
Since abccmeeqk
start with a
qe have to put aaa
in front of, we now got : aaaabccmeeqk bbb
FOUR : get your result
solution_cuount = factorials_quotient * factorial(number of sub-string in our string)
In our case : aaaabccmeeqk bbb
we got 2 sub-string.
solution_count = 4 * 2! = 4 * 2
= 8 solutions :
Possible solutions :
aa a abc cme eqk bb b
aa a abc cme eqk b bb
a aa abc cme eqk bb b
a aa abc cme eqk b bb
bb b aa a abc cme eqk
b bb aa a abc cme eqk
bb b a aa abc cme eqk
b bb a aa abc cme eqk
Array step by step :
STEP 0: (at start)
array(
"aa",
"a",
"abc",
"cme",
"eqk",
"bb",
"b"
)
STEP 1:
array(
"aa",
"a",
"abccmeeqk",
"bb",
"b"
)
STEP 2:
array(
"aaa",
"abccmeeqk",
"bbb"
)
STEP 3:
array(
"aaaabccmeeqk",
"bbb"
)
STEP 4:
you got your result