-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay22.java
More file actions
46 lines (45 loc) · 1.21 KB
/
Copy pathDay22.java
File metadata and controls
46 lines (45 loc) · 1.21 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
//Problem:minimum add to make parentheses valid
//https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/description/
class Solution {
public int minAddToMakeValid(String s) {
int open=0, close=0;
for (char ch : s.toCharArray()) {
if (ch == '(') {
open++;
} else {
if (open > 0) {
open--;
} else {
close++;
}
}
}
return open + close;
}
}
//TC:O(N)
//SC:O(1)
//Problem:Sum of beauty of all substrings
//https://leetcode.com/problems/sum-of-beauty-of-all-substrings/description/
class Solution {
public int beautySum(String s) {
int i,ans=0;
for(i=0;i<s.length();i++){
int[] freq=new int[26];
for(int j=i;j<s.length();j++){
int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
freq[s.charAt(j)-'a']++;
for(int k:freq){
if(k>0){
max=Math.max(max,k);
min=Math.min(min,k);
}
}
ans=ans+(max-min);
}
}
return ans;
}
}
//TC:O(n^2)
//SC:O(1)