返回介绍

solution / 0700-0799 / 0777.Swap Adjacent in LR String / README_EN

发布于 2024-06-17 01:03:34 字数 4268 浏览 0 评论 0 收藏 0

777. Swap Adjacent in LR String

中文文档

Description

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'.

Solutions

Solution 1

class Solution:
  def canTransform(self, start: str, end: str) -> bool:
    n = len(start)
    i = j = 0
    while 1:
      while i < n and start[i] == 'X':
        i += 1
      while j < n and end[j] == 'X':
        j += 1
      if i >= n and j >= n:
        return True
      if i >= n or j >= n or start[i] != end[j]:
        return False
      if start[i] == 'L' and i < j:
        return False
      if start[i] == 'R' and i > j:
        return False
      i, j = i + 1, j + 1
class Solution {
  public boolean canTransform(String start, String end) {
    int n = start.length();
    int i = 0, j = 0;
    while (true) {
      while (i < n && start.charAt(i) == 'X') {
        ++i;
      }
      while (j < n && end.charAt(j) == 'X') {
        ++j;
      }
      if (i == n && j == n) {
        return true;
      }
      if (i == n || j == n || start.charAt(i) != end.charAt(j)) {
        return false;
      }
      if (start.charAt(i) == 'L' && i < j || start.charAt(i) == 'R' && i > j) {
        return false;
      }
      ++i;
      ++j;
    }
  }
}
class Solution {
public:
  bool canTransform(string start, string end) {
    int n = start.size();
    int i = 0, j = 0;
    while (true) {
      while (i < n && start[i] == 'X') ++i;
      while (j < n && end[j] == 'X') ++j;
      if (i == n && j == n) return true;
      if (i == n || j == n || start[i] != end[j]) return false;
      if (start[i] == 'L' && i < j) return false;
      if (start[i] == 'R' && i > j) return false;
      ++i;
      ++j;
    }
  }
};
func canTransform(start string, end string) bool {
  n := len(start)
  i, j := 0, 0
  for {
    for i < n && start[i] == 'X' {
      i++
    }
    for j < n && end[j] == 'X' {
      j++
    }
    if i == n && j == n {
      return true
    }
    if i == n || j == n || start[i] != end[j] {
      return false
    }
    if start[i] == 'L' && i < j {
      return false
    }
    if start[i] == 'R' && i > j {
      return false
    }
    i, j = i+1, j+1
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文