-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2337.java
More file actions
58 lines (46 loc) · 1.44 KB
/
LC2337.java
File metadata and controls
58 lines (46 loc) · 1.44 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
51
52
53
54
55
56
57
58
/*
* LC2237
*/
import java.util.*;
public class LC2337 {
public static boolean canChange(String start, String target) {
int i = 0;
int j = 0;
int n = start.length();
while (i < n || j < n) {
// skip all blanks in start
while (i < n && start.charAt(i) == '_') {
i++;
}
// skip all blanks in target
while (j < n && target.charAt(j) == '_') {
j++;
}
// count is same only if both the string end at the same time
if (i == n || j == n) {
return i == n && j == n;
}
// check false scenario
if (start.charAt(i) != target.charAt(j) || (start.charAt(i) == 'L' && j > i)
|| (start.charAt(i) == 'R' && j < i)) {
return false;
}
// move to the next character after comparison
i++;
j++;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter The Start String : ");
String start = sc.nextLine();
System.out.println();
System.out.print("Enter The Target String : ");
String target = sc.nextLine();
System.out.println();
boolean ans = canChange(start, target);
System.out.println(ans);
sc.close();
}
}