5ms Java with Explanations
-- What does either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR" mean?\nL and R cannot go across each other, that i
-- What does `either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR"` mean?\nL and R cannot go across each other, that is, their relative order in a string doesn\'t change.\nMeanwhile, we should satisfy either one of cases below: \n```\ncase 1 : L = start[i] = end[j] and i >= j\ncase 2 : R = start[i] = end[j] and i <= j\n```\nfor i, j are the index of next non-X character in start and end separately.\n\n```\nFor example,\n-- i = 0, j = 1 satisfies case 2\nRXXL\ni\nXRLX\n j\n-- i = 3, j = 2 satisfies case 1\nRXXL\n   i\nXRLX\n  j\n```\nIn order to transfer start to end, they should own same number of Ls and Rs as well. That is, when one run out of non-Xs, the other cannot have non-Xs.\n****\n```\n    public boolean canTransform(String start, String end) {       \n        int i = 0, j = 0, len = start.length();\n        \n        while (i < len && j < len) {\n            while (i < len && start.charAt(i) == \'X\')\n                i++;\n            while (j < len && end.charAt(j) == \'X\')\n                j++;   \n            if (i < len && j < len) {\n                char chS = start.charAt(i);\n                char chE = end.charAt(j);\n                if ((chS != chE) || (chS == \'L\' && i < j) || (chS == \'R\' && i > j))\n                    return false;\n                i++;\n                j++;\n            }\n        }\n        \n        while (i < len) {\n            if (start.charAt(i) != \'X\')\n                return false;\n            i++;\n        }\n        \n        while (j < len) {\n            if (end.charAt(j) != \'X\')\n                return false;\n            j++;\n        }\n        \n        return true;\n    }\n```\n**(\uFF89>\u03C9<)\uFF89 Vote up, please!**
[]