Skip to content

Commit 45e55ef

Browse files
solves maximum score after splitting a string
1 parent d8a77f2 commit 45e55ef

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@
360360
| 1408 | [String Matching In An Array](https://leetcode.com/problems/string-matching-in-an-array) | [![Java](assets/java.png)](src/StringMatchingInAnArray.java) | |
361361
| 1413 | [Minimum Value To Get Positive Step By Step Sum](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum) | [![Java](assets/java.png)](src/MinimumValueToGetPositiveStepByStepSum.java) | |
362362
| 1417 | [Reformat The String](https://leetcode.com/problems/reformat-the-string) | [![Java](assets/java.png)](src/ReformatTheString.java) | |
363-
| 1422 | [Maximum Score After Splitting A String](https://leetcode.com/problems/maximum-score-after-splitting-a-string) | | |
363+
| 1422 | [Maximum Score After Splitting A String](https://leetcode.com/problems/maximum-score-after-splitting-a-string) | [![Java](assets/java.png)](src/MaximumScoreAfterSplittingAString.java) | |
364364
| 1426 | [Counting Elements](https://leetcode.com/problems/counting-elements) | | |
365365
| 1427 | [Performing String Shifts](https://leetcode.com/problems/perform-string-shifts) | | |
366366
| 1431 | [Kids With The Greatest Number Of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies) | | |
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class MaximumScoreAfterSplittingAString {
2+
public int maxScore(String s) {
3+
final int ones = getFrequency(s, '1');
4+
int score = 0;
5+
for (int index = 0, left = 0, right = ones ; index < s.length() - 1 ; index++) {
6+
left += s.charAt(index) == '0' ? 1 : 0;
7+
right -= s.charAt(index) == '0' ? 0 : 1;
8+
score = Math.max(score, left + right);
9+
}
10+
return score;
11+
}
12+
13+
private int getFrequency(String s, char character) {
14+
int frequency = 0;
15+
for (int index = 0 ; index < s.length() ; index++) {
16+
if (s.charAt(index) == character) frequency++;
17+
}
18+
return frequency;
19+
}
20+
}

0 commit comments

Comments
 (0)