-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
58 lines (45 loc) · 1.35 KB
/
Solution.java
File metadata and controls
58 lines (45 loc) · 1.35 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
package leetcode.maximumScoreFromRemovingSubstrings;
public class Solution {
public int maximumGain(String s, int x, int y) {
int score = 0;
char[] chars = s.toCharArray();
int length = chars.length;
char ch1 = 'a', ch2 = 'b';
int count1 = 0, count2 = 0;
if( x < y ){
int temp = x;
x = y;
y = temp;
ch1 = 'b';
ch2 = 'a';
}
for(int i = 0; i < length; i++){
char character = chars[i];
if(character == ch1){
count1++;
} else if(character == ch2){
if(count1 > 0){
score += x;
count1--;
} else {
count2++;
}
} else {
score += Math.min(count1, count2) * y;
count1 = 0;
count2 = 0;
}
}
if(count1 != 0){
score += Math.min(count1, count2) * y;
}
return score;
}
public static void main(String[] args) {
Solution s = new Solution();
double resultExpected = 19;
double result = s.maximumGain("cdbcbbaaabab", 4, 5);
System.out.println("RESULT EXPECTED: " + resultExpected);
System.out.println("RESULT: " + result );
}
}