[Swift]LeetCode777. 在LR字符串中交换相邻字符 | 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‘}.


在一个由 ‘L‘ , ‘R‘ 和 ‘X‘ 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作。一次移动操作指用一个"LX"替换一个"XL",或者用一个"XR"替换一个"RX"。现给定起始字符串start和结束字符串end,请编写代码,当且仅当存在一系列移动操作使得start可以转换成end时, 返回True

示例 :

输入: start = "RXXLRXRXL", end = "XRLXXRRLX"
输出: True
解释:
我们可以通过以下几步将start转换成end:
RXXLRXRXL ->
XRXLRXRXL ->
XRLXRXRXL ->
XRLXXRRXL ->
XRLXXRRLX

注意:

  1. 1 <= len(start) = len(end) <= 10000
  2. startend中的字符串仅限于‘L‘‘R‘‘X‘


Runtime: 24 ms

Memory Usage: 19.5 MB

 1 class Solution {
 2     func canTransform(_ start: String, _ end: String) -> Bool {
 3         var start = Array(start)
 4         var end = Array(end)
 5         var n:Int = start.count
 6         var cntL:Int = 0
 7         var cntR:Int = 0
 8         for i in 0..<n
 9         {
10             if start[i] == "R" {cntR += 1}
11             if end[i] == "L" {cntL += 1}
12             if start[i] == "L" {cntL -= 1}
13             if end[i] == "R" {cntR -= 1}
14             if cntL < 0 || cntR < 0 || cntL * cntR != 0
15             {
16                 return false
17             }
18         }
19         return cntL == 0 && cntR == 0
20     }
21 }


68ms

 1 class Solution {
 2     func canTransform(_ start: String, _ end: String) -> Bool {
 3         let cs = Array(start)
 4         let ce = Array(end)
 5         let ts = String(cs.filter{ $0 != "X" } )
 6         let te = String(ce.filter{ $0 != "X" } )
 7
 8         guard ts == te else { return false }
 9
10         var sr = [Int]()
11         var sl = [Int]()
12
13         for (i, char) in cs.enumerated() {
14             if char == "R" {
15                 sr.append(i)
16             } else if char == "L" {
17                 sl.append(i)
18             }
19         }
20
21         var rCount = 0
22         var lCount = 0
23
24         for (i, char) in ce.enumerated() {
25             if char == "R" {
26                 if sr[rCount] > i {
27                     return false
28                 } else {
29                     rCount += 1
30                 }
31             } else if char == "L" {
32                 if sl[lCount] < i {
33                     return false
34                 } else {
35                     lCount += 1
36                 }
37             }
38         }
39
40         return true
41     }
42 }


96ms

 1 class Solution {
 2     func compare(_ start: String, _ end: String) -> Bool {
 3         var rs1 = 0
 4         var ls1 = 0
 5         var xs1 = 0
 6         var rs2 = 0
 7         var ls2 = 0
 8         var xs2 = 0
 9         for i in start.indices {
10             switch start[i] {
11                 case "R":
12                     rs1 += 1
13                 case "L":
14                     ls1 += 1
15                 case "X":
16                     xs1 += 1
17                 default:
18                     break
19             }
20             switch end[i] {
21                 case "R":
22                     rs2 += 1
23                 case "L":
24                     ls2 += 1
25                 case "X":
26                     xs2 += 1
27                 default:
28                     break
29             }
30         }
31         return rs1 == rs2 && ls1 == ls2 && xs1 == xs2
32     }
33
34     func canTransform(_ start: String, _ end: String) -> Bool {
35         var rs = [String.Index]()
36         var ls = [String.Index]()
37         for (i, ch) in zip(start.indices, start) {
38             if ch == "R" {
39                 rs.append(i)
40             } else if ch == "L" {
41                 ls.append(i)
42             }
43         }
44         rs.reverse()
45         ls.reverse()
46
47         if !compare(start, end) {
48             return false
49         }
50
51         for (i, ch) in zip(end.indices, end) {
52             if ch == "R" {
53                 if let nextRIdx = rs.last, nextRIdx <= i {
54                     if let nextLIdx = ls.last {
55                         if nextLIdx < nextRIdx {
56                             // impossible to move R through L
57                             return false
58                         } else {
59                             rs.removeLast()
60                         }
61                     } else {
62                         rs.removeLast()
63                     }
64                 } else {
65                     return false
66                 }
67             } else if ch == "L" {
68                 if let nextLIdx = ls.last, nextLIdx >= i {
69                     if let nextRIdx = rs.last {
70                         if nextRIdx < nextLIdx {
71                             // impossible to move L through R
72                             return false
73                         } else {
74                             ls.removeLast()
75                         }
76                     } else {
77                         ls.removeLast()
78                     }
79                 } else {
80                     return false
81                 }
82             }
83         }
84         return true
85     }
86 }


104ms

 1 class Solution {
 2     func canTransform(_ start: String, _ end: String) -> Bool {
 3         guard start.count >= 1 && start.count <= 10000 && end.count >= 1 && end.count <= 10000 else {
 4             return false
 5         }
 6         let startChars = Array(start), endChars = Array(end)
 7         var startInfos = [(Character, Int)](), endInfos = [(Character, Int)]()
 8         for (index, char) in startChars.enumerated() {
 9             if char == "L" || char == "R" {
10                 startInfos.append((char, index))
11             } else if char != "X" {
12                 return false
13             }
14         }
15         for (index, char) in endChars.enumerated() {
16             if char == "L" || char == "R" {
17                 endInfos.append((char, index))
18             } else if char != "X" {
19                 return false
20             }
21         }
22         let startInfosChars = startInfos.map {
23             $0.0
24         }
25         let endInfosChars = endInfos.map {
26             $0.0
27         }
28         guard startInfosChars == endInfosChars else {
29             return false
30         }
31         for (index, startInfo) in startInfos.enumerated() {
32             let endInfo = endInfos[index]
33             if startInfo.0 == "L" {
34                 guard startInfo.1 >= endInfo.1 else {
35                     return false
36                 }
37             } else {
38                 guard startInfo.1 <= endInfo.1 else {
39                     return false
40                 }
41             }
42         }
43         return true
44     }
45 }

原文地址:https://www.cnblogs.com/strengthen/p/10541711.html

时间: 2024-07-31 00:54:21

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

777.在LR字符串中交换相邻字符

在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作.一次移动操作指用一个"LX"替换一个"XL",或者用一个"XR"替换一个"RX".现给定起始字符串start和结束字符串end,请编写代码,当且仅当存在一系列移动操作使得start可以转换成end时, 返回True. 示例 : 输入: start = "RXXLRXRXL", en

转:假设有一个字符串aabcad,请编写一段程序,去掉字符串中不相邻的重复字符。

假设有一个字符串aabcad,请编写一段程序,去掉字符串中不相邻的重复字符.即上述字串处理之后结果是为:aabcd; 分析,重点考查 char 与int 的隐式转换.程序如下: -(void) removeRepeat:(NSString *)aNum { NSMutableArray *mArr = [[NSMutableArray alloc]initWithCapacity:10]; for(int i = 0; i<aNum.length; i++) { [mArr addObject:

双指针---反转字符串中的元音字符

反转字符串中的元音字符 345. Reverse Vowels of a String (Easy) Given s = "leetcode", return "leotcede" 题目描述: ??给定一个字符串,将字符串中的元音字母交换,返回交换后的字符串. 思路分析: ??使用双指针指向待反转的两个元音字符,一个指针从头向尾进行遍历,一个指针从尾到头遍历. 代码: private final static HashSet<Character>vowe

css如何设置字符串中第一个字符的样式

css如何设置字符串中第一个字符的样式:本章节介绍一下如何使用css设置字符串中第一个字符的样式.以前我们实现此效果的方式,可能会在第一个字符上嵌套上一个span标签.代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/"

Java 求字符串中出现频率最高字符

前段时间接触的这个题目,大体理解了,还有些小地方仍待进一步品味,暂且记下. import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /* * 查找字符串中出现频率最高的字符 * * 主要思路:先将字符

*字符串-01. 在字符串中查找指定字符

1 /* 2 * Main.c 3 * D1-字符串-01. 在字符串中查找指定字符 4 * Created on: 2014年8月18日 5 * Author: Boomkeeper 6 *****部分通过****** 7 */ 8 9 #include <stdio.h> 10 11 int mysearch(char ch, const char str[], int length) { 12 13 int j, ret = -1; 14 15 for (j = 0; j < le

JavaScript替换字符串中最后一个字符

1.问题背景 在一个输入框中,限制字符串长度为12位.利用键盘输入一个数字,会将字符串中最后一位替换,比方:111111111111.再输入一个3,会显示111111111113 2.详细实现 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html x

[算法]删除字符串中重复的字符

如何删除字符串中重复的字符 问题描述: 删除字符串中重复的字符,例如,"good"去掉重复的字符串后就变成"god". 第一种方法: "蛮力法",最简单的方法就是把这个字符串看作是一个字符数组,对该数组使用双重循环进行遍历,如果发现有重复的字符,就把该字符置为'\0',最后再把这个字符数组中所有的'\0'去掉,此时得到的字符串就是删除重复字符后的目标字符串. 代码如下: package 删除字符串中重复的字符; public class Solu

JAVA----编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符

package com.pb.demo.packclass.demo1; import java.util.HashSet; /** * 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符 例如: 原始字符串是"abc",打印得到下列所有组合情况 "a" "b" "c" "ab" "bc" "ca" "ba" "cb"