// 此时已保证去掉下划线后的所有字符相等,现在只需检查相对顺序即可 inti=0, j = 0; while (i < start.length()) { while (i < start.length() && start.charAt(i) == '_') { i++; } // 寻找到 L 或 R if (i == start.length()) { returntrue; } while (target.charAt(j) == '_') { j++; }
if (i != j) { // 需要保证可移动 if (start.charAt(i) == 'L' && i < j) { // 因为 L 只能向左移动,所以此处返回 false returnfalse; } if (start.charAt(i) == 'R' && i > j) { // 因为 R 只能向右移动,所以此处返回 false returnfalse; } }