• 777. 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 end, return True if and only if there exists a sequence of moves to transform one string to the other.

    Example:

    Input: start = "RXXLRXRXL", end = "XRLXXRRLX"
    Output: True
    Explanation:
    We can transform start to end following these steps:
    RXXLRXRXL ->
    XRXLRXRXL ->
    XRLXRXRXL ->
    XRLXXRRXL ->
    XRLXXRRLX

    Note:

    1. 1 <= len(start) = len(end) <= 10000.
    2. Both start and end will only consist of characters in {'L', 'R', 'X'}.
    class Solution:
        def canTransform(self, start, end):
            """
            :type start: str
            :type end: str
            :rtype: bool
            """
            if len(start)!=len(end):
                return False
            l = len(start)
            i = j = 0
            while i<l and j<l:
                while j<l and end[j]=='X':
                    j += 1              #去掉X
                while i<l and start[i]=='X':
                    i += 1             #去掉X
                if i==l and j==l:           #都走到最后
                    break
                if i==l or j==l or end[j]!=start[i]:   #只有一方走到最后、或者R对应上了L
                    return False
                if start[i]=='L' and i<j:   #L只能向左走
                    return False
                if start[i]=='R' and i>j:     #R只能向右走
                    return False
                j += 1
                i += 1
            return True
    
  • 相关阅读:
    在登陆脚本中设置自动打开某个网页
    修改2k自动登陆的脚本
    COM组件和NT服务
    HRMS提示"HRMS服务器未注册或注册不正确"问题
    登录脚本
    vb的GUID生成算法
    WIN2000管理员密码的解密
    注册表中相等的项
    统一管理Windows 2000域中的服务
    深入挖掘Windows脚本技术
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/9782786.html
Copyright © 2020-2023  润新知