Skip to content

Commit 3f71969

Browse files
Sync LeetCode submission Runtime - 18 ms (31.48%), Memory - 19.2 MB (48.41%)
1 parent 50e6375 commit 3f71969

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
class Solution {
22
public:
33
int countCollisions(string d) {
4-
int n=d.size(), ans=n, l=0, r=0, s=0;
5-
for (auto &i : d) s += (i=='S');
6-
for (int i=0; i<n; i++) {
7-
if (d[i] != 'L') break;
8-
l++;
4+
int ans = 0;
5+
int rights = 0;
6+
bool any = false;
7+
for (char c : d) {
8+
if (c == 'R') {
9+
rights++;
10+
any = true;
11+
}
12+
else if (c == 'L') {
13+
if (rights) ans += rights + 1;
14+
else if (any) ans++;
15+
rights = 0;
16+
}
17+
else {
18+
ans += rights;
19+
rights = 0;
20+
any = true;
21+
}
922
}
10-
for (int i=n-1; i>=0; i--) {
11-
if (d[i] != 'R') break;
12-
r++;
13-
}
14-
return ans - l - r - s;
23+
return ans;
1524
}
1625
};

0 commit comments

Comments
 (0)