g0701_0800.s0777_swap_adjacent_in_lr_string.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0701_0800.s0777_swap_adjacent_in_lr_string;
// #Medium #String #Two_Pointers #2022_03_26_Time_4_ms_(85.13%)_Space_44.1_MB_(54.82%)
/**
* 777 - Swap Adjacent in LR String\.
*
* Medium
*
* 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 `end`, return `True` if and only if there exists a sequence of moves to transform one string to the other.
*
* **Example 1:**
*
* **Input:** start = "RXXLRXRXL", end = "XRLXXRRLX"
*
* **Output:** true
*
* **Explanation:**
*
* We can transform start to end following these steps:
* RXXLRXRXL ->
* XRXLRXRXL ->
* XRLXRXRXL ->
* XRLXXRRXL ->
* XRLXXRRLX
*
* **Example 2:**
*
* **Input:** start = "X", end = "L"
*
* **Output:** false
*
* **Constraints:**
*
* * 1 <= start.length <= 104
* * `start.length == end.length`
* * Both `start` and `end` will only consist of characters in `'L'`, `'R'`, and `'X'`.
**/
public class Solution {
public boolean canTransform(String start, String end) {
int i1 = 0;
int i2 = 0;
char[] s1 = start.toCharArray();
char[] s2 = end.toCharArray();
while (i1 < s1.length && i2 < s2.length) {
while (i1 < s1.length && s1[i1] == 'X') {
i1++;
}
while (i2 < s2.length && s2[i2] == 'X') {
i2++;
}
if (i1 == s1.length && i2 == s2.length) {
return true;
}
if (i1 == s1.length || i2 == s2.length) {
return false;
}
char c1 = s1[i1];
char c2 = s2[i2];
if (c1 != c2) {
return false;
}
if (c1 == 'L' && i1 < i2) {
return false;
}
if (c1 == 'R' && i1 > i2) {
return false;
}
i1++;
i2++;
}
while (i1 < s1.length && start.charAt(i1) == 'X') {
i1++;
}
while (i2 < s2.length && end.charAt(i2) == 'X') {
i2++;
}
return i1 == s1.length && i2 == s2.length;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy