Skip to content

Commit 1a02270

Browse files
committed
check if set of moves is circular
1 parent 18649ce commit 1a02270

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- [x] [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)
3939
- [x] [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/)
4040
- [x] [Valid Parentheses](https://leetcode.com/problems/valid-parentheses)
41+
- [x] [Check if given set of moves is circular or not](http://www.techiedelight.com/check-given-set-moves-circular-not/)
4142

4243
### Dates
4344

@@ -313,7 +314,6 @@
313314
- [ ] [Greedy coloring of graph](http://www.techiedelight.com/greedy-coloring-graph/)
314315

315316
- LinkedLists
316-
- [ ] [Reverse a LL](https://leetcode.com/problems/reverse-linked-list/)
317317
- [ ] [Detect Cycle](https://leetcode.com/problems/linked-list-cycle/)
318318
- [ ] [Merge 2 Sorted](https://leetcode.com/problems/merge-two-sorted-lists/)
319319
- [ ] [Merge k Sorted](https://leetcode.com/problems/merge-k-sorted-lists/)
@@ -384,7 +384,6 @@
384384
- [ ] [Find path from source to destination in a matrix that satisfies given constraints](http://www.techiedelight.com/find-path-source-destination-matrix-satisfies-given-constraints/)
385385
- Strings
386386
- [ ] [KMP](https://www.wikiwand.com/en/articles/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm)
387-
- [ ] [Check if given set of moves is circular or not](http://www.techiedelight.com/check-given-set-moves-circular-not/)
388387
- [ ] [Check if given string is a rotated palindrome or not](http://www.techiedelight.com/check-given-string-rotated-palindrome-not/)
389388
- [ ] [Check if repeated subsequence is present in the string or not](http://www.techiedelight.com/check-repeated-subsequence-present-string-not/)
390389
- [ ] [Check if strings can be derived from each other by circularly rotating them](http://www.techiedelight.com/check-strings-can-derived-circularly-rotating/)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package strings.circularMoves;
2+
3+
import utils.InputReader;
4+
5+
public class CircularMoves {
6+
7+
static int x = 0;
8+
static int y = 0;
9+
static Direction direction = Direction.N; // start looking at north
10+
11+
public static void main(String[] args) {
12+
// input
13+
System.out.print("Print set of moves: M for same direction, L for left, R for right: ");
14+
String moves = InputReader.readLine();
15+
16+
// validate input
17+
if (!moves.matches("^[LMR]*$\n")) {
18+
System.err.println("Invalid input");
19+
}
20+
21+
// check
22+
if (isCircular(moves.toUpperCase().trim())) {
23+
System.out.println("Set of moves " + moves + " is circular");
24+
} else {
25+
System.out.println("Set of moves " + moves + " is NOT circular");
26+
}
27+
}
28+
29+
// start at 0,0, facing UP
30+
private static boolean isCircular(String moves) {
31+
for (char c : moves.toCharArray()) {
32+
switch (c) {
33+
case 'L':
34+
// System.out.println("going Left");
35+
if (direction.equals(Direction.N)) {
36+
direction = Direction.W;
37+
} else if (direction.equals(Direction.W)) {
38+
direction = Direction.S;
39+
} else if (direction.equals(Direction.S)) {
40+
direction = Direction.E;
41+
} else if (direction.equals(Direction.E)) {
42+
direction = Direction.N;
43+
}
44+
break;
45+
case 'R':
46+
// System.out.println("going right");
47+
if (direction.equals(Direction.N)) {
48+
direction = Direction.E;
49+
} else if (direction.equals(Direction.E)) {
50+
direction = Direction.S;
51+
} else if (direction.equals(Direction.S)) {
52+
direction = Direction.W;
53+
} else if (direction.equals(Direction.W)) {
54+
direction = Direction.N;
55+
}
56+
break;
57+
case 'M':
58+
// System.out.println("going forward");
59+
if (direction.equals(Direction.N)) {
60+
y++;
61+
} else if (direction.equals(Direction.S)) {
62+
y--;
63+
} else if (direction.equals(Direction.E)) {
64+
x++;
65+
} else if (direction.equals(Direction.W)) {
66+
x--;
67+
}
68+
break;
69+
default: // will never reach here
70+
System.err.println("Invalid set of moves");
71+
}
72+
}
73+
return (x == 0 && y == 0); // if eventually we're back at 0,0, it is circular
74+
}
75+
76+
private enum Direction {
77+
N, E, S, W;
78+
}
79+
}

0 commit comments

Comments
 (0)