forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1221.java
46 lines (44 loc) · 1.18 KB
/
_1221.java
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
package com.fishercoder.solutions;
public class _1221 {
public static class Solution1 {
public int balancedStringSplit(String s) {
int i = 0;
int balancedCount = 0;
int lCount = 0;
int rCount = 0;
while (i < s.length()) {
if (s.charAt(i) == 'L') {
lCount++;
} else {
rCount++;
}
i++;
if (lCount != 0 && lCount == rCount) {
lCount = 0;
rCount = 0;
balancedCount++;
}
}
return balancedCount;
}
}
public static class Solution2 {
public int balancedStringSplit(String s) {
int count = 0;
int result = 0;
int i = 0;
while (i < s.length()) {
if (s.charAt(i) == 'L') {
count++;
} else {
count--;
}
if (count == 0) {
result++;
}
i++;
}
return result;
}
}
}