zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    wireShark 代码分析
    Flex Chart / Charting 图表参考
    Boost笔记
    mysql的常用开发工具【建模、维护、监控】
    DSL应用集成和Rhino 3
    元编程 Metaprogramming
    Coffeescript的使用简要
    Ruby基础[Programing ruby笔记]
    编程范式/范型参考 programming paradigm
    DSL语法、组成 2
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/9782786.html
Copyright © 2011-2022 走看看