Skip to content

Commit cd3c7b9

Browse files
authored
Create Count Collisions on a Road.java
1 parent 06bb8bc commit cd3c7b9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int countCollisions(String directions) {
3+
int idx = 0;
4+
while (idx < directions.length() && directions.charAt(idx) == 'L') {
5+
idx++;
6+
}
7+
int carsFromRight = 0;
8+
int collisionCount = 0;
9+
while (idx < directions.length()) {
10+
if (directions.charAt(idx) == 'R') {
11+
carsFromRight++;
12+
} else {
13+
collisionCount += directions.charAt(idx) == 'S' ? carsFromRight : carsFromRight + 1;
14+
carsFromRight = 0;
15+
}
16+
idx++;
17+
}
18+
return collisionCount;
19+
}
20+
}

0 commit comments

Comments
 (0)