-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_56.java
More file actions
49 lines (44 loc) · 1.28 KB
/
Copy pathDay_56.java
File metadata and controls
49 lines (44 loc) · 1.28 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
// Remove Outermost Parentheses
class Solution {
public String removeOuterParentheses(String s) {
int n = s.length();
StringBuilder sb = new StringBuilder();
int op = 0, cl = 0, i = 0;
for(int j=0;j<n;j++){
if(s.charAt(j) == '(') op++;
else cl++;
if(op == cl){
sb.append(s.substring(i+1,j));
i = j + 1;
op = 0;
cl = 0;
}
}
return sb.toString();
}
}
// Smallest distinct window
class Solution {
public int findSubString(String str) {
int n = str.length();
int mcnt = Integer.MAX_VALUE, l = 0,cnt = 0;
Set<Character> st = new HashSet<>();
for(int i=0;i<n;i++){
st.add(str.charAt(i));
}
int ucnt = st.size();
int[] freq = new int[26];
for(int r=0;r<n;r++){
char ch = str.charAt(r);
if(freq[ch-'a'] == 0) cnt++;
freq[ch-'a']++;
if(ucnt == cnt) mcnt = Math.min(mcnt,r-l+1);
while(ucnt == cnt &&freq[str.charAt(l)-'a']>1){
freq[str.charAt(l)-'a']--;
l++;
mcnt = Math.min(mcnt,r-l+1);
}
}
return mcnt;
}
}