793. Swap Adjacent in LR String
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks if we can transform a start string into a result string using a specific set of moves. The moves are: XL can become LX, and RX can become XR. Let's analyze these moves:
Solution
Swap Adjacent in LR String in Python
class Solution:
def canTransform(self, start: str, result: str) -> bool:
n = len(start)
i, j = 0, 0
while True:
# Find the next non-'X' character in start
while i < n and start[i] == 'X':
i += 1
# Find the next non-'X' character in result
while j < n and result[j] == 'X':
j += 1
# If both pointers reach the end, all non-'X' chars have been processed successfully.
if i == n and j == n:
return True
# If one pointer reaches the end but the other doesn't,
# they have different numbers of non-'X' characters.
if i == n or j == n:
return False
# The non-'X' characters must be the same (preserves relative order).
if start[i] != result[j]:
return False
# Check movement constraints
# 'L' can only move left, so its start index must be >= its result index.
if start[i] == 'L' and i < j:
return False
# 'R' can only move right, so its start index must be <= its result index.
if start[i] == 'R' and i > j:
return False
# Move to the next characters to compare.
i += 1
j += 1
Swap Adjacent in LR String in Java
class Solution {
public boolean canTransform(String start, String result) {
int n = start.length();
int i = 0, j = 0;
while (true) {
// Find the next non-'X' character in start
while (i < n && start.charAt(i) == 'X') {
i++;
}
// Find the next non-'X' character in result
while (j < n && result.charAt(j) == 'X') {
j++;
}
// If both pointers reach the end, all non-'X' chars have been processed successfully.
if (i == n && j == n) {
return true;
}
// If one pointer reaches the end but the other doesn't,
// they have different numbers of non-'X' characters.
if (i == n || j == n) {
return false;
}
// The non-'X' characters must be the same (preserves relative order).
if (start.charAt(i) != result.charAt(j)) {
return false;
}
// Check movement constraints
// 'L' can only move left, so its start index must be >= its result index.
if (start.charAt(i) == 'L' && i < j) {
return false;
}
// 'R' can only move right, so its start index must be <= its result index.
if (start.charAt(i) == 'R' && i > j) {
return false;
}
// Move to the next characters to compare.
i++;
j++;
}
}
}
Swap Adjacent in LR String in C++
class Solution {
public:
bool canTransform(string start, string result) {
int n = start.length();
int i = 0, j = 0;
while (true) {
// Find the next non-'X' character in start
while (i < n && start[i] == 'X') {
i++;
}
// Find the next non-'X' character in result
while (j < n && result[j] == 'X') {
j++;
}
// If both pointers reach the end, all non-'X' chars have been processed successfully.
if (i == n && j == n) {
return true;
}
// If one pointer reaches the end but the other doesn't,
// they have different numbers of non-'X' characters.
if (i == n || j == n) {
return false;
}
// The non-'X' characters must be the same (preserves relative order).
if (start[i] != result[j]) {
return false;
}
// Check movement constraints
// 'L' can only move left, so its start index must be >= its result index.
if (start[i] == 'L' && i < j) {
return false;
}
// 'R' can only move right, so its start index must be <= its result index.
if (start[i] == 'R' && i > j) {
return false;
}
// Move to the next characters to compare.
i++;
j++;
}
}
};
Swap Adjacent in LR String in TypeScript
function canTransform(start: string, result: string): boolean {
const n = start.length;
let i = 0, j = 0;
while (true) {
// Find the next non-'X' character in start
while (i < n && start[i] === 'X') {
i++;
}
// Find the next non-'X' character in result
while (j < n && result[j] === 'X') {
j++;
}
// If both pointers reach the end, all non-'X' chars have been processed successfully.
if (i === n && j === n) {
return true;
}
// If one pointer reaches the end but the other doesn't,
// they have different numbers of non-'X' characters.
if (i === n || j === n) {
return false;
}
// The non-'X' characters must be the same (preserves relative order).
if (start[i] !== result[j]) {
return false;
}
// Check movement constraints
// 'L' can only move left, so its start index must be >= its result index.
if (start[i] === 'L' && i < j) {
return false;
}
// 'R' can only move right, so its start index must be <= its result index.
if (start[i] === 'R' && i > j) {
return false;
}
// Move to the next characters to compare.
i++;
j++;
}
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.