作者: 负雪明烛
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. dokuwiki使用随笔

    1. 在dokuwiki上安装MataJax插件后: a. $....$:之间书写数学公式;如完全平方公式:$a^2+b^2+2ab$,该公式将与当前行文字处于同一行; b. $$ .... $$ 之 ...

  2. 23-Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  3. ES5中改变this指向的三种方法

    ES5中提供了三种改变函数中this指针指向的方法,分别如下 1.call() var obj = {username:"孙悟空"}; //没有任何修饰的调用函数,函数中的this ...

  4. 13个酷炫的JavaScript一行程序

    1. 获得一个随机的布尔值(true/false) const randomBoolean = () => Math.random() >= 0.5; console.log(random ...

  5. Mac iOS区别

    iOS是从MacOS衍生出来的专门未为移动设备(iphone)推出的操作系统.iOS和Mac OS的UI观念有很大差别,iOS主要支持手势操作,包括多触点技术等等.从开发人员观点看,iOS和macos ...

  6. Mysql的表级锁

    我们首先需要知道的一个大前提是:mysql的锁是由具体的存储引擎实现的.所以像Mysql的默认引擎MyISAM和第三方插件引擎 InnoDB的锁实现机制是有区别的.可根据不同的场景选用不同的锁定机制. ...

  7. react-native环境搭建完后,用genymotion运行出错的处理方法

    以下方法是争对react-native  0.63版本的 出错提示如下: 模拟器点击reload后,如下提示: 找了网上很多方法,很多都是旧版本的bug处理的方法,没有用,后面经过摸索发现,原来原因是 ...

  8. 注册页面的servlet

    package cn.itcast.travel.web.servlet;import cn.itcast.travel.domain.ResultInfo;import cn.itcast.trav ...

  9. rpm-build方式制作rpm包

    目录 一.简介 二.具体操作 一.简介 可以将编译完成的服务打成rpm包放到私有仓库了,用于自定义的各种软件进行安装部署配置. 二.具体操作 1.安装软件,这个命令将构建rpm包 yum -y ins ...

  10. 模糊C均值算法

    Fuzzy C-Means读书笔记 一.算法简介 很显然,图中的数据集可分为两个簇.借鉴K-Means算法的思想,利用单个特殊的点(质心)表示一个簇.因此,我们用\(C_1\)和\(C_2\)分别表示 ...