777. Swap Adjacent in LR String LeetCode Solution
In this guide, you will get 777. Swap Adjacent in LR String LeetCode Solution with the best time and space complexity. The solution to Swap Adjacent in LR String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Swap Adjacent in LR String solution in C++
- Swap Adjacent in LR String solution in Java
- Swap Adjacent in LR String solution in Python
- Additional Resources
Problem Statement of Swap Adjacent in LR String
In a string composed of ‘L’, ‘R’, and ‘X’ characters, like “RXXLRXRXL”, a move consists of either replacing one occurrence of “XL” with “LX”, or replacing one occurrence of “RX” with “XR”. Given the starting string start and the ending string result, return True if and only if there exists a sequence of moves to transform start to result.
Example 1:
Input: start = “RXXLRXRXL”, result = “XRLXXRRLX”
Output: true
Explanation: We can transform start to result following these steps:
RXXLRXRXL ->
XRXLRXRXL ->
XRLXRXRXL ->
XRLXXRRXL ->
XRLXXRRLX
Example 2:
Input: start = “X”, result = “L”
Output: false
Constraints:
1 <= start.length <= 104
start.length == result.length
Both start and result will only consist of characters in 'L', 'R', and 'X'.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
777. Swap Adjacent in LR String LeetCode Solution in C++
class Solution {
public:
bool canTransform(string start, string end) {
if (removeX(start) != removeX(end))
return false;
int i = 0; // start's index
int j = 0; // end's index
while (i < start.length() && j < end.length()) {
while (i < start.length() && start[i] == 'X')
++i;
while (j < end.length() && end[j] == 'X')
++j;
if (i == start.length() && j == end.length())
return true;
if (i == start.length() || j == end.length())
return false;
// L can only move to left.
if (start[i] == 'L' && i < j)
return false;
// R can only move to right.
if (start[i] == 'R' && i > j)
return false;
++i;
++j;
}
return true;
}
private:
string removeX(const string& s) {
string t = s;
std::erase(t, 'X');
return t;
}
};
/* code provided by PROGIEZ */
777. Swap Adjacent in LR String LeetCode Solution in Java
class Solution {
public boolean canTransform(String start, String end) {
if (!start.replace("X", "").equals(end.replace("X", "")))
return false;
int i = 0; // start's index
int j = 0; // end's index
while (i < start.length() && j < end.length()) {
while (i < start.length() && start.charAt(i) == 'X')
++i;
while (j < end.length() && end.charAt(j) == 'X')
++j;
if (i == start.length() && j == end.length())
return true;
if (i == start.length() || j == end.length())
return false;
// L can only move to left.
if (start.charAt(i) == 'L' && i < j)
return false;
// R can only move to right.
if (start.charAt(i) == 'R' && i > j)
return false;
++i;
++j;
}
return true;
}
}
// code provided by PROGIEZ
777. Swap Adjacent in LR String LeetCode Solution in Python
class Solution:
def canTransform(self, start: str, end: str) -> bool:
if start.replace('X', '') != end.replace('X', ''):
return False
i = 0 # start's index
j = 0 # end's index
while i < len(start) and j < len(end):
while i < len(start) and start[i] == 'X':
i += 1
while j < len(end) and end[j] == 'X':
j += 1
if i == len(start) and j == len(end):
return True
if i == len(start) or j == len(end):
return False
# L can only move to left.
if start[i] == 'L' and i < j:
return False
# R can only move to right.
if start[i] == 'R' and i > j:
return False
i += 1
j += 1
return True
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.