Instead of repeating all possible permutations, suppose that exists and is trying to build it. I believe that this is done correctly, the failure of the algorithm would imply the existence of a permutation.
Here is the outline of the idea applied to the above expressions:
let:
str1 = "A m * B s / (A m + C m)" str2 = "C m * B s / (C m + A m)"
We are looking for a permutation of the set (A, C), which will make the expressions identical. Relabel A and C as X1 and X2 according to the order of their first appearance in str2, therefore:
X1 = C X2 = A
because C appears before A in str2. Then create an array Y such that y [i] is the ith character of A or C in the order of the first occurrence in str1. So:
Y[1] = A Y[2] = C
Since A appears before C in str1.
Now build str3 from str2, replacing A and C with X1 and X2:
str3 = "X1 m * B s / (X1 m + X2 m)"
And then start replacing Xi for Y [i]. First, X1 becomes Y [1] = A:
str3_1 = "A m * Bs / (A m + X2 m)"
At this point, compare str3_1 and str1 with the first occurrence of any of Xi, in this case X2, since these two lines are equal:
str3_1[:18] = "A m * B s / (A m + " str1[:18] = "A m * B s / (A m + "
You have a chance to build a permutation. If they were unequal, you would not prove that a suitable permutation does not exist (because any permutation would have to make at least this replacement) and could deduce nonequivalence. But they are equal, so go to the next step, replacing X2 with Y [2] = C:
str3_2 = "A m * B s / (A m + C m)"
And this equals str1, so you have your permutation (A-> C, C-> A) and have shown the equivalence of the expressions.
This is only a demonstration of the algorithm for a specific case, but it should generalize. Not sure which lowest order you could get, but it should be faster than n! generating all permutations on n variables.
If I understand the meaning of units correctly, they limit which variables can be swapped and others can be swapped. Thus, A can be replaced by C in the above expressions, because both have units "m", but not with B, which have "s" units. You can handle this as follows:
build the expressions str1_m and str2_m from str1 and str2, removing all characters that do not have m units, and then execute the above algorithm for str1_m and str2_m. If the build fails, the permutation does not exist. If the build is successful, save this permutation (call it m-permutation) and build str1_s and str2_s from str1 and str2, deleting all characters that don't have s-units, then run the algorithm again for str1_s and str2_s. If construction is not completed, they are not equivalent. If that succeeds, the final permutation will be a combination of m-permutation and s-permutation (although you probably don't even need to create it, you just need it to exist).