Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static long getMaxCost(String s) {
- long cost = 0;
- long ones = 0;
- long zeroes = 0;
- // Remove leading zeros and append "1" to handle final group
- s = s.replaceAll("^0+", "") + "1";
- for (char ch : s.toCharArray()) {
- if (ch == '1') {
- if (zeroes > 0) {
- cost += ones * (zeroes + 1);
- }
- ones += 1;
- zeroes = 0;
- } else {
- zeroes += 1;
- }
- }
- return cost;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement