-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCanTransform.java
More file actions
50 lines (50 loc) · 1.12 KB
/
CanTransform.java
File metadata and controls
50 lines (50 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public boolean canTransform(String start, String end) {
// First, remove all 'X', and compare if they equal.
StringBuffer s = new StringBuffer();
StringBuffer e = new StringBuffer();
for (char c : start.toCharArray()) {
if (c != 'X') {
s.append(c);
}
}
for (char c : end.toCharArray()) {
if (c != 'X') {
e.append(c);
}
}
if (!s.toString().equals(e.toString()))
return false;
// check R
// form i to start.length(),
// if R found in start count++,
// if R found in end count--,
// if count comes to negative, that means 'R' is in more left position in end, so 'R' can't move from start to end.
int count = 0;
for (int i = 0; i < start.length(); i++) {
if (start.charAt(i) == 'R') {
count++;
}
else if (end.charAt(i) == 'R') {
count--;
}
if (count < 0) {
return false;
}
}
// check L
// same with checking 'R', only difference is:
// to ensure every 'L' comes first in end.
count = 0;
for (int i = 0; i < start.length(); i++) {
if (end.charAt(i) == 'L') {
count++;
}
else if (start.charAt(i) == 'L') {
count--;
}
if (count < 0) {
return false;
}
}
return true;
}