-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack1221.java
More file actions
68 lines (67 loc) · 1.83 KB
/
Stack1221.java
File metadata and controls
68 lines (67 loc) · 1.83 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
59
60
61
62
63
64
65
66
67
68
package stack;
/**
* @ProjectName: leetcode
* @Package: stack
* @ClassName: Stack1221
* @Author: markey
* @Description:
* 在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的。
*
* 给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。
*
* 返回可以通过分割得到的平衡字符串的最大数量。
*
*
*
* 示例 1:
*
* 输入:s = "RLRRLLRLRL"
* 输出:4
* 解释:s 可以分割为 "RL", "RRLL", "RL", "RL", 每个子字符串中都包含相同数量的 'L' 和 'R'。
* 示例 2:
*
* 输入:s = "RLLLLRRRLR"
* 输出:3
* 解释:s 可以分割为 "RL", "LLLRRR", "LR", 每个子字符串中都包含相同数量的 'L' 和 'R'。
* 示例 3:
*
* 输入:s = "LLLLRRRR"
* 输出:1
* 解释:s 只能保持原样 "LLLLRRRR".
*
*
* 提示:
*
* 1 <= s.length <= 1000
* s[i] = 'L' 或 'R'
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/split-a-string-in-balanced-strings
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2019/12/1 21:11
* @Version: 1.0
*/
public class Stack1221 {
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Split a String in Balanced Strings.
* Memory Usage: 34.1 MB, less than 100.00% of Java online submissions for Split a String in Balanced Strings.
* @param s
* @return
*/
public int balancedStringSplit(String s) {
int num = 0;
int left = 0, right = 0;
for (char c: s.toCharArray()) {
if (c == 'L') {
left++;
}
if (c == 'R') {
right++;
}
if (left > 0 && left == right) {
num++;
}
}
return num;
}
}