作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/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:

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’}.

题目大意

给出了一个初始的字符串,可以把初始字符串中的XL换成LX,可以把初始字符串中的RX换成XR,求问能不能通过一定的次数之后,把初始字符串变成结束字符串。

解题方法

智商题

本来以为是类似于迷宫的搜索问题,看到数据规模这么大,感觉不像,一看tag果然是智商题,那我就放弃了,智商不够用啊。

题目说了可以把初始字符串中的XL换成LX,可以把初始字符串中的RX换成XR,那么也就是说初始字符串中的L只能向左移动,初始字符串中R只能向右移动。并且L和R是不可能交换顺序的,两个字符串的L和R对应次数应该相等。知道这个规律之后,就使用双指针去解决就好了。

用i,j分别指向start和end的起始位置,如果他们指向的是X,那么都向右移动到不是X的位置,那么他们指向的字符应该相等,否则返回False。如果指向的是L,那么,start中的L的索引一定只能比end中L的索引小;如果指向的是R,那么,end中的R的索引一定只能比end中R的索引大。在while的结束之前需要把i和j同时向后移动。

全部判断完成之后返回True.

时间复杂度是O(N),空间复杂度是O(1).

class Solution(object):
def canTransform(self, start, end):
"""
:type start: str
:type end: str
:rtype: bool
"""
i, j = 0, 0
N = len(start)
while i < N and j < N:
while i < N - 1 and start[i] == 'X':
i += 1
while j < N - 1 and end[j] == 'X':
j += 1
if 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 += 1
j += 1
return True

相似题目

参考资料

http://www.cnblogs.com/grandyang/p/9001474.html

日期

2018 年 10 月 30 日 —— 啊,十月过完了

【LeetCode】777. Swap Adjacent in LR String 解题报告(Python)的更多相关文章

  1. [LeetCode] Swap Adjacent in LR String 交换LR字符串中的相邻项

    In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of ...

  2. 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  3. [Swift]LeetCode777. 在LR字符串中交换相邻字符 | Swap Adjacent in LR String

    In a string composed of 'L', 'R', and 'X'characters, like "RXXLRXRXL", a move consists of ...

  4. 【LeetCode】758. Bold Words in String 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  5. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...

  7. 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  8. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

  9. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

随机推荐

  1. CentOS6.9 内核升级详解

    内核进行的是应用软件和计算机硬件的交互工作在计算机科学中,内核(英语:kernel)又称核心,是一个计算机程序,用来管理软件发出的数据I/O(输入与输出)要求,将这些要求转译为数据处理的指令,交由中央 ...

  2. Linux搭建rsync备份服务器备份

    环境: 1台rsync备份服务器,IP:10.0.0.188 1台rsync备份客户端,IP:10.0.0.51 备份数据需注意: 1. 在业务低谷时间进行备份 2. 进行备份限速 一.搭建rsync ...

  3. C#集合Dictionary中按值的排序

    C#集合Dictionary中按值的降序排列 static void Main(string[] args) {             Dictionary<string, int> d ...

  4. 使用clion阅读eos源码

    配置mingw 安装clion 从github克隆源码 使用clion open打开 在cmake上使用boost: sudo apt-get install libboost-all-dev

  5. Spark(二十)【SparkSQL将CSV导入Kudu】

    目录 SparkSql 将CSV导入kudu pom 依赖 scala 代码 启动脚本 SparkSql 将CSV导入kudu pom 依赖 <properties> <spark. ...

  6. 零基础学习java------day8------javabean编写规范,继承,static关键字,代码块,单例设计模式

    0. 今日内容提要 1. javabean书写规范 javabean:一个普通的类,用来描述事物的类,里面不包含任何的业务逻辑,只是用来存储数据. 比如:Teacher,Student,Mobile. ...

  7. HTML5 之 FileReader 的使用 (二) (网页上图片拖拽并且预显示可在这里学到) [转载]

    转载至 : http://www.360doc.com/content/14/0214/18/1457948_352511645.shtml FileReader 资料(英文): https://de ...

  8. HTML5 之 FileReader 的使用 (网页上图片拖拽并且预显示可在这里学到) [转载]

    转载至 : http://www.360doc.com/content/14/0214/18/1457948_352511416.shtml FileReader 资料(英文) : https://d ...

  9. Docker学习(三)——Docker镜像使用

    Docker镜像使用     当运行容器时,使用的镜像如果在本地中不存在,docker就会自动从docker镜像仓库中下载,默认是从Docker Hub公共镜像源下载. 1.镜像使用     (1)列 ...

  10. IDEA2021.2安装与配置

    https://blog.csdn.net/qq_37242720/article/details/119349394