【LeetCode】777. Swap Adjacent in LR String 解题报告(Python)
作者: 负雪明烛
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 <= len(start) = len(end) <= 10000.
- 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)的更多相关文章
- [LeetCode] Swap Adjacent in LR String 交换LR字符串中的相邻项
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of ...
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- [Swift]LeetCode777. 在LR字符串中交换相邻字符 | Swap Adjacent in LR String
In a string composed of 'L', 'R', and 'X'characters, like "RXXLRXRXL", a move consists of ...
- 【LeetCode】758. Bold Words in String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
随机推荐
- Macbook pro进入恢复模式以及无法进入恢复模式解决方案
看网上很多说用Command+R进入恢复模式,但是,大部分都反馈说,此命令并不能进入恢复模式.我自己也尝试发现了同样问题,最终发现解决方案: 问题出在,[是重新启动电脑,而不是关机+按开机键,否则会造 ...
- Demo02一千以内的水仙花数
package 习题集2;//1000以内的水仙花数public class Demo02 { public static void main(String[] args) { int i = 100 ...
- 学习java 7.20
学习内容: Stream流 Stream流的生成方式 中间操作方法 终结操作方法 Stream流的收集操作 类加载 类加载器的作用 将.class文件加载到内存中,并为之生成对应的java.lang. ...
- Scala(三)【函数式编程】
目录 一.方法和函数 1.方法 1)基本语法 2)简化原则 3)方法参数 2.函数 3.方法和函数的区别 二.高阶函数 三.匿名函数 四.柯里化 五.闭包 一.方法和函数 1.方法 1)基本语法 de ...
- Angular Service设计理念及使用
官方认为组件不应该直接获取或保存数据, 它们应该聚焦于展示数据,而把数据访问的职责委托给某个服务. 而服务就充当着数据访问,逻辑处理的功能.把组件和服务区分开,以提高模块性和复用性. 1.依赖注入 注 ...
- 【bfs】洛谷 P1443 马的遍历
题目:P1443 马的遍历 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 记录一下第一道ac的bfs,原理是利用队列queue记录下一层的所有点,然后一层一层遍历: 其中: 1.p ...
- Oracle中如何自定义类型
一:Oracle中的类型有很多种,主要可以分为以下几类:1.字符串类型.如:char.nchar.varchar2.nvarchar2.2.数值类型.如:int.number(p,s).integer ...
- OC-私有方法,构造方法,类的本质及启动过程
总结 标号 主题 内容 一 OC的私有方法 私有变量/私有方法 二 @property 概念/基本使用/寻找方法的过程/查找顺序 三 @synthesize @synthesize概念/基本使用/注意 ...
- 【编程思想】【设计模式】【行为模式Behavioral】command
Python版 https://github.com/faif/python-patterns/blob/master/behavioral/command.py #!/usr/bin/env pyt ...
- 【Java 8】Stream.distinct() 列表去重示例
在这篇文章里,我们将提供Java8 Stream distinct()示例. distinct()返回由该流的不同元素组成的流.distinct()是Stream接口的方法. distinct()使用 ...