classSolution: defcanChange(self, start: str, target: str) -> bool: n = len(start) i = j = 0 while i < n and j < n: while i < n and start[i] == '_': i += 1 while j < n and target[j] == '_': j += 1 if i < n and j < n: if start[i] != target[j]: returnFalse c = start[i] if c == 'L'and i < j or c == 'R'and i > j: returnFalse i += 1 j += 1 while i < n: if start[i] != '_': returnFalse i += 1 while j < n: if target[j] != '_': returnFalse j += 1 returnTrue
publicclassSolution { publicboolCanChange(string start, string target) { int n = start.Length; int i = 0, j = 0; while (i < n && j < n) { while (i < n && start[i] == '_') { i++; } while (j < n && target[j] == '_') { j++; } if (i < n && j < n) { if (start[i] != target[j]) { returnfalse; } char c = start[i]; if ((c == 'L' && i < j) || (c == 'R' && i > j)) { returnfalse; } i++; j++; } } while (i < n) { if (start[i] != '_') { returnfalse; } i++; } while (j < n) { if (target[j] != '_') { returnfalse; } j++; } returntrue; } }
classSolution { public: boolcanChange(string start, string target){ int n = start.length(); int i = 0, j = 0; while (i < n && j < n) { while (i < n && start[i] == '_') { i++; } while (j < n && target[j] == '_') { j++; } if (i < n && j < n) { if (start[i] != target[j]) { returnfalse; } char c = start[i]; if ((c == 'L' && i < j) || (c == 'R' && i > j)) { returnfalse; } i++; j++; } } while (i < n) { if (start[i] != '_') { returnfalse; } i++; } while (j < n) { if (target[j] != '_') { returnfalse; } j++; } returntrue; } };
boolcanChange(char * start, char * target) { int n = strlen(start); int i = 0, j = 0; while (i < n && j < n) { while (i < n && start[i] == '_') { i++; } while (j < n && target[j] == '_') { j++; } if (i < n && j < n) { if (start[i] != target[j]) { returnfalse; } char c = start[i]; if ((c == 'L' && i < j) || (c == 'R' && i > j)) { returnfalse; } i++; j++; } } while (i < n) { if (start[i] != '_') { returnfalse; } i++; } while (j < n) { if (target[j] != '_') { returnfalse; } j++; } returntrue; }
var canChange = function(start, target) { const n = start.length; let i = 0, j = 0; while (i < n && j < n) { while (i < n && start[i] === '_') { i++; } while (j < n && target[j] === '_') { j++; } if (i < n && j < n) { if (start[i] !== target[j]) { returnfalse; } const c = start[i]; if ((c === 'L' && i < j) || (c === 'R' && i > j)) { returnfalse; } i++; j++; } } while (i < n) { if (start[i] !== '_') { returnfalse; } i++; } while (j < n) { if (target[j] !== '_') { returnfalse; } j++; } returntrue; };
funccanChange(start string, target string)bool { i, j, n := 0, 0, len(start) for i < n && j < n { for i < n && start[i] == '_' { i++ } for j < n && target[j] == '_' { j++ } if i < n && j < n { if start[i] != target[j] { returnfalse } c := start[i] if c == 'L' && i < j || c == 'R' && i > j { returnfalse } i++ j++ } } for i < n { if start[i] != '_' { returnfalse } i++ } for j < n { if target[j] != '_' { returnfalse } j++ } returntrue }
复杂度分析
时间复杂度:O(n),其中 n 是字符串 start 和 target 的长度。需要遍历两个字符串各一次。