Skip to content

Commit 4e98492

Browse files
committed
777_Swap_Adjacent_in_LR_String
1 parent 6e07981 commit 4e98492

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
836836
|13| **[319. Bulb Switcher](https://tinyurl.com/ycygg2ju)** | [Python](https://tinyurl.com/wu6rdaw/319_Bulb_Switcher.py)| [Art 1](https://tinyurl.com/y886rzfe), [Art 2](https://tinyurl.com/y9n7vo2x), [Art 3](https://tinyurl.com/ya3fookw) | Medium (Really!!??) | Are you fucking kidding me!! We are programmers, not math wizard. |
837837
|14| **[1344. Angle Between Hands of a Clock](https://tinyurl.com/yy3g8mg6)** | [Python](https://tinyurl.com/wu6rdaw/1344_Angle_Between_Hands_of_a_Clock.py), [Swift](https://tinyurl.com/wuja3c4/1344_Angle_Between_Hands_of_a_Clock.swift)| --- | Medium | FB really likes to ask tricky question. |
838838
|15| **[1276. Number of Burgers with No Waste of Ingredients](https://tinyurl.com/y4dl7mne)** | [Python](https://tinyurl.com/wu6rdaw/1276_Number_of_Burgers_with_No_Waste_of_Ingredients.py), [Swift](https://tinyurl.com/wuja3c4/1276_Number_of_Burgers_with_No_Waste_of_Ingredients.swift)| [Art 1](https://tinyurl.com/y2u668aa), [Art 2](https://tinyurl.com/y2u9nvre), [Art 3](https://tinyurl.com/yxdzbp9s) | Medium | Pure math |
839+
|16| **[777. Swap Adjacent in LR String](https://tinyurl.com/y2syu722)** | [Python](https://tinyurl.com/wu6rdaw/777_Swap_Adjacent_in_LR_String.py), [Swift](https://tinyurl.com/wuja3c4/777_Swap_Adjacent_in_LR_String.swift)| [Art 1](https://tinyurl.com/y6cfe8gy) | Medium | Brainteser |
839840

840841
</p>
841842
</details>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import Foundation
2+
3+
// My solution during MOC. Wrong answer
4+
class Solution {
5+
func canTransform(_ start: String, _ end: String) -> Bool {
6+
var start = Array(start)
7+
var end = Array(end)
8+
guard start.count == end.count else {
9+
return false
10+
}
11+
12+
var start_ptr = 0
13+
while start_ptr < start.count {
14+
if start[start_ptr] != end[start_ptr] {
15+
if start_ptr + 1 < start.count && ((start[start_ptr] == "X" && start[start_ptr + 1] == "L") || (start[start_ptr] == "R" && start[start_ptr + 1] == "X")) {
16+
start.swapAt(start_ptr, start_ptr + 1)
17+
if start[start_ptr] == end[start_ptr] && start[start_ptr + 1] == end[start_ptr + 1] {
18+
start_ptr += 1
19+
} else {
20+
return false
21+
}
22+
} else {
23+
return false
24+
}
25+
}
26+
start_ptr += 1
27+
}
28+
return true
29+
}
30+
}
31+
32+
// Source: https://tinyurl.com/y6cfe8gy
33+
// Accepted
34+
class Solution {
35+
func canTransform(_ start: String, _ end: String) -> Bool {
36+
var startArray = Array(start)
37+
var endArray = Array(end)
38+
39+
guard startArray.count == endArray.count else {
40+
return false
41+
}
42+
43+
if start.replacingOccurrences(of: "X", with: "") != end.replacingOccurrences(of: "X", with: "") {
44+
return false
45+
}
46+
47+
var (L_start, L_end) = ([Int](), [Int]())
48+
var (R_start, R_end) = ([Int](), [Int]())
49+
50+
for i in 0..<startArray.count {
51+
if startArray[i] == "L" {
52+
L_start.append(i)
53+
}
54+
if endArray[i] == "L" {
55+
L_end.append(i)
56+
}
57+
58+
if startArray[i] == "R" {
59+
R_start.append(i)
60+
}
61+
if endArray[i] == "R" {
62+
R_end.append(i)
63+
}
64+
}
65+
66+
for (i, j) in zip(L_start, L_end) {
67+
if i < j {
68+
return false
69+
}
70+
}
71+
72+
for (i, j) in zip(R_start, R_end) {
73+
if i > j {
74+
return false
75+
}
76+
}
77+
return true
78+
}
79+
}

0 commit comments

Comments
 (0)