Skip to content

Commit d00bf46

Browse files
763_Partition_Level.java
1 parent 49f6123 commit d00bf46

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

JAVA/763_Partition_Level.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Problem Number: 763
2+
3+
// Partition Level
4+
5+
class Solution {
6+
public List<Integer> partitionLabels(String s) {
7+
List<Integer> ans = new ArrayList<>();
8+
int[] rightmost = new int[26];
9+
10+
for (int i = 0; i < s.length(); ++i)
11+
rightmost[s.charAt(i) - 'a'] = i;
12+
13+
int l = 0; // the leftmost index of the current running string
14+
int r = 0; // the rightmost index of the current running string
15+
16+
for (int i = 0; i < s.length(); ++i) {
17+
r = Math.max(r, rightmost[s.charAt(i) - 'a']);
18+
if (r == i) {
19+
ans.add(i - l + 1);
20+
l = i + 1;
21+
}
22+
}
23+
return ans;
24+
}
25+
}

0 commit comments

Comments
 (0)