# Code language: Python classSolution: defcanTransform(self, start: str, end: str) -> bool: if start.count('X') != end.count('X'): returnFalse defget(s): # 可以压缩为 yield from ((i, c) for i, c in enumerate(s) if c != 'X') for i, c inenumerate(s): if c != 'X': yield i, c for (i, c1), (j, c2) inzip(get(start), get(end)): if c1 != c2: returnFalse if c1 == 'L'and j > i: returnFalse if c1 == 'R'and j < i: returnFalse returnTrue
// Code language: C++ classSolution { public: boolcanTransform(string start, string end){ int n = start.size(); for (int i = 0, j = 0; i < n || j < n; ++i, ++j) { while (i < n && start[i] == 'X') ++i; while (j < n && end[j] == 'X') ++j; // 数量不一致 if (i == n && j != n) returnfalse; if (i != n && j == n) returnfalse; if (i == n && j == n) returntrue; // 相对位置不一致 if (start[i] != end[j]) returnfalse; // 相对位置不匹配 if (start[i] == 'L' && j > i) returnfalse; if (start[i] == 'R' && j < i) returnfalse; } returntrue; } };