Cody

Problem 2324. GJam 2014 Rd 1c: Train Cars

This Challenge is derived from GJam 2014 Rd 1c: Reordering Train Cars.

Determine number of sequences for set of strings under the constraint that all same characters must be contiguous.

Input: s, string of N space separated string segments of letters [a..z]. 1<=N<=10. Total letters <=100.

Output: val, number of possible sequences

Example: Small Case

ab bbbc cd Val=1 as only abbbbccd can be created
aa aa bc c Val=4 aa gives 2 positions, aa'aa''bcc,aa''aa'bcc, bcccaa'aa'',bcccaa''aa'
abc bcd    Val=0 as c is internal and thus can not connect to c of abc

Theory: (Spoilers)

A methodical approach implements the following checks: No internal equals any Start/End. Note aaa has no internal. Verify each string has no non-contiguous letters. Verify no two strings have same start or end except where start==end as in bbbb. Val is N! if there are N cc strings. Each string segment is considered a unique piece when counting. Reduce the strings of type aa until there is only one and increase Val by N!. With remaining strings merge to S strings. Val is then scaled by S!. Key merging issue is that ab ba may look mergeable to aa but in actuality it creates abba - invalid and baab -invalid thus Val=0. Creation of full length string and then a final validity check resolves this issue.

Additional GJam solutions can be found at Example GJam Matlab solutions. Select Find Solutions, change Language to Matlab. The Test Suite, at the bottom, contains a full GJam Matlab solution. No Valid Matlab solutions were submitted during the contest.

Solution Stats

90.91% Correct | 9.09% Incorrect
Last solution submitted on Dec 15, 2018

Solution Comments