MATLAB - 75 98 bytes
Note: I didn't see that you had to output True
or False
, so I had to create an additional cell array that stores these two choices then we choose from this array when we're done.
This is a rather brute-force solution. This assumes that you have loaded A
in as a character matrix and that A
is symmetric in size (i.e. # of rows is equal to # of columns):
c={'False','True'};o=0;for k=perms(1:size(A,1))'o=o|sum(sum(A(k,:)==A(k,:)'))==numel(A);end,c{o+1}
The overall process is to first create a 2D array of all possible permutations that goes from 1 up to as many words as we have N
. Each row of this matrix gives you a permutation enumerated from 1 up to N
, and we can check either the number of rows or columns for the value N
. Next, we go through every possible permutation and rearrange the matrix A
's rows based on the permutation vector produced by that permutation. For example, if N=4
, and if we had [1,4,2,3]
as the permutation vector, it would rearrange the matrix A
so that row 1 of the original matrix A
appears first, followed by row 4 of the original matrix A
appearing second, row 2 of the original matrix A
appearing third and finally row 3 of the original matrix A
appearing last.
The above problem is essentially checking to see that if we all of these matrix rearrangements for A
over all possible permutations, we check to see if this rearranged matrix is equal to its transpose. In order to facilitate this, I do an element-by-element equality check of the characters, and sum over this result. If the sum is equal to the total size of the matrix, then we have a true result. This code will check all possible permutations, even if we have found a match before we exhaust all possibilities. The statement o=0
is an indicator variable o
that tells us whether we have found the condition being true or false.
Inside the loop, we keep updating o
and checking for the above summation condition by logical ORing with the current result and with the current rearranged matrix condition. It will be set to true
if we have found such a condition or it gets to 1, and will stay false
if we don't, or stays at 0.
Minor Note
The for
loop behaviour is rather undocumented, but if you specify a matrix to be what is iterating over the for
loop, each value k
would be a column of the said matrix. For example, if we did:
B = [0 0 0 0; 1 1 1 1; 2 2 2 2; 3 3 3 3];
for k = B
disp(k);
end
We would select out the first column of B
, display it, then the next column of B
, display it, etc... so you would get a series of [0,1,2,3]'
s.
Example Runs
>> A = ['FILE'; 'ICED'; 'LEAD'; 'EDDY'];
>> c={'False','True'};o=0;for k=perms(1:size(A,1))'o=o|sum(sum(A(k,:)==A(k,:)'))==numel(A);end,c{o+1}
ans =
True
>> A = ['FILE'; 'LEAD'; 'EDDY'; 'ICED'];
>> c={'False','True'};o=0;for k=perms(1:size(A,1))'o=o|sum(sum(A(k,:)==A(k,:)'))==numel(A);end,c{o+1}
ans =
True
>> A = ['OGRE'; 'GAIA'; 'RIGS'; 'EASY'];
>> c={'False','True'};o=0;for k=perms(1:size(A,1))'o=o|sum(sum(A(k,:)==A(k,:)'))==numel(A);end,c{o+1}
ans =
True
>> A = ['CAMP', 'AXEL', 'MEMO', 'PLOW'];
>> c={'False','True'};o=0;for k=perms(1:size(A,1))'o=o|sum(sum(A(k,:)==A(k,:)'))==numel(A);end,c{o+1}
ans =
True
>> A = ['SPASM', 'PASTA', 'ASPEN', 'STEEL', 'MANLY'];
>> c={'False','True'};o=0;for k=perms(1:size(A,1))'o=o|sum(sum(A(k,:)==A(k,:)'))==numel(A);end,c{o+1}
ans =
True
Just to be sure, here's an example of where it shouldn't work:
>> A = ['CBMP'; 'APEL'; 'MCTO'; 'DLOX'];
>> A
A =
CBMP
APEL
MCTO
DLOX
>> c={'False','True'};o=0;for k=perms(1:size(A,1))'o=o|sum(sum(A(k,:)==A(k,:)'))==numel(A);end,c{o+1}
ans =
False
True
/False
while others have them natively. \$\endgroup\$