-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_41.java
More file actions
46 lines (43 loc) · 1.04 KB
/
Copy pathDay_41.java
File metadata and controls
46 lines (43 loc) · 1.04 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
// Valid Parenthesis String
class Solution {
public boolean checkValidString(String s) {
int min = 0, max = 0;
for(var ch : s.toCharArray()){
if(ch == '('){
min++;
max++;
}
else if(ch == ')'){
min--;
max--;
}
else{
min--;
max++;
}
if(min<0) min = 0;
if(max<0) return false;
}
return (min == 0);
}
}
// Partition Labels
class Solution {
public List<Integer> partitionLabels(String s) {
List<Integer> ans = new ArrayList<>();
int n = s.length();
int[] last = new int[26];
for(int i=0;i<n;i++){
last[s.charAt(i)-'a'] = i;
}
int st = 0, end = 0;
for(int i=0;i<n;i++){
end = Math.max(end,last[s.charAt(i)-'a']);
if(end == i){
ans.add(end-st+1);
st = end + 1;
}
}
return ans;
}
}