[LeetCode] Swap Adjacent in LR String 交换LR字符串中的相邻项
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'}
.
这道题给了我们一个只含有L,R,X三个字符的字符串,然后说有两种操作,一种是把 "XL" 变成 "LX",另一种是把 "RX" 变成 "XR"。博主刚开始没有读题意,以为二者是可以互换的,错误的认为认为 "LX" 也能变成 "XL",其实题目这种变换是单向,这种单向关系就是解题的关键,具体来说,就是要把start字符串变成end字符串的话,L只能往前移动,因为是把 "XL" 变成 "LX",同样,R只能往后移动,因为是把 "RX" 变成 "XR"。题目给的那个例子并不能很好的说明问题,博主之前那种双向变换的错误认知会跪在这个例子:
start = "XXRXXLXXXX"
end = "XXXXRXXLXX"
我们观察这个test case,可以发现start中的R可以往后移动,没有问题,但是start中的L永远无法变到end中L的位置,因为L只能往前移。这道题被归类为brainteaser,估计就是因为要观察出这个规律吧。那么搞明白这个以后,我们其实可以用双指针来解题,思路是,我们每次分别找到start和end中非X的字符,如果二者不相同的话,直接返回false,想想问什么?这是因为不论是L还是R,其只能跟X交换位置,L和R之间是不能改变相对顺序的,所以如果分别将start和end中所有的X去掉后的字符串不相等的话,那么就永远无法让start和end相等了。这个判断完之后,就来验证L只能前移,R只能后移这个限制条件吧,当i指向start中的L时,那么j指向end中的L必须要在前面,所以如果i小于j的话,就不对了,同理,当i指向start中的R,那么j指向end中的R必须在后面,所以i大于j就是错的,最后别忘了i和j同时要自增1,不然死循环了,参见代码如下:
解法一:
class Solution {
public:
bool canTransform(string start, string end) {
int n = start.size(), i = , j = ;
while (i < n && j < n) {
while (i < n && start[i] == 'X') ++i;
while (j < n && end[j] == 'X') ++j;
if (start[i] != end[j]) return false;
if ((start[i] == 'L' && i < j) || (start[i] == 'R' && i > j)) return false;
++i; ++j;
}
return true;
}
};
下面这种解法也挺巧妙的,这里我们使用两个计数器cntL和cntR,分别来统计L和R出现的次数,统计方法时,start中出现了L或R,计数器自增1,end中出现了L或R,计数器自减1。注意我们检测的顺序很重要,由于start中的R必须在end中的R前面,所以我们要先检测start中的R,同理,由于end中的L必须要在start中的L前面,所以我们要先检测end中的L,那么四个if写完后,如果cntL或者cntR中有任何一个小于0了,说明限制条件被打破了,返回false,或者当二者都大于0的时候,说明此时不匹配了,参见上面解法中对于去掉所有的X的解释,一个道理,说明L和R的相对顺序不同了,那么也是false。最终for循环退出后,如果cntL和cntR均为0的时候,才返回true,否则就是false,参见代码如下:
解法二:
class Solution {
public:
bool canTransform(string start, string end) {
int n = start.size(), cntL = , cntR = ;
for (int i = ; i < n; ++i) {
if (start[i] == 'R') ++cntR;
if (end[i] == 'L') ++cntL;
if (start[i] == 'L') --cntL;
if (end[i] == 'R') --cntR;
if (cntL < || cntR < || cntL * cntR != ) return false;
}
return cntL == && cntR == ;
}
};
参考资料:
https://leetcode.com/problems/swap-adjacent-in-lr-string/solution/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Swap Adjacent in LR String 交换LR字符串中的相邻项的更多相关文章
- [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- [LeetCode] 438. Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)
题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- [Swift通天遁地]五、高级扩展-(14)扩展String快速计算字符串中的各种数学表达式
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- DirectX11 With Windows SDK--21 鼠标拾取
前言 拾取是一项非常重要的技术,不论是电脑上用鼠标操作,还是手机的触屏操作,只要涉及到UI控件的选取则必然要用到该项技术.除此之外,一些类似魔兽争霸3.星际争霸2这样的3D即时战略游戏也需要通过拾取技 ...
- 如何解决failed to push some refs to git
$ git push -u origin master To git@github.com:yangchao0718/cocos2d.git ! [rejected] master -& ...
- 异常捕获try----catch
如果try语句里有return,返回的是try语句块中变量值. 详细执行过程如下: 如果有返回值,就把返回值保存到局部变量中: 执行jsr指令跳到finally语句里执行: 执行完finally语句后 ...
- sort与uniq命令详解
1.sort的作用 (排序) sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出. 如果 File 参数指定多个文件,那么 sort 命令将这些文件连接起来,并当作一个文件进行 ...
- 【转载】使用sklearn优雅地进行数据挖掘
原文:http://www.cnblogs.com/jasonfreak/p/5448462.html 目录 1 使用sklearn进行数据挖掘 1.1 数据挖掘的步骤 1.2 数据初貌 1.3 关键 ...
- JVM--02
Java虚拟机内存管理: 共享: 方法区:存储运行时常量池.已被虚拟机加载的类信息.常量.静态变量.即时编译器编译后的代码等数据 java堆:存储对象实例 线程独占区: 虚拟机栈:存放方法运行时所需的 ...
- 手机端调用app导航
因为是在微信端中操作,所以只能使用腾讯地图才能调起手机的腾讯地图app <!doctype html> <html> <head> <meta charset ...
- 图形验证码 tesserocr pillow
利用tesserocr和pil生成图形验证码 import tesserocr from PIL import Image image = Image.open('222.jpg') image = ...
- 初识C语言(一)
C语言的结构体 一个C程序就是由多个头文件和函数组成 #include<stdio.h> /* 包含头文件*/ int main() { printf('"hello world ...
- java 根据Url下载对应的文件到指定位置,读txt文件获取url
package test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...