Skip to content

Commit 61d41a9

Browse files
committed
Add 1422. Maximum Score After Splitting a String
1 parent 58f5e0b commit 61d41a9

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package sierikov.leetcode.tasks.easy
2+
3+
object MaximumScoreAfterSplittingString extends App {
4+
5+
def maxScore(s: String): Int = s.toList match
6+
case Nil => 0
7+
case _ :: Nil => 1
8+
case c :: rest => {
9+
10+
var lScore = if (c == '0') 1 else 0
11+
var rScore = rest.count(_ == '1')
12+
var max = lScore + rScore
13+
14+
rest.init.foreach {
15+
case '0' =>
16+
lScore += 1
17+
max = math.max(max, lScore + rScore)
18+
case '1' =>
19+
if (rScore > 0) rScore -= 1
20+
max = math.max(max, lScore + rScore)
21+
}
22+
max
23+
}
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package sierikov.leetcode.tasks.easy
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
6+
class MaximumScoreAfterSplitingStringTest extends AnyFlatSpec with Matchers {
7+
8+
import MaximumScoreAfterSplittingString._
9+
10+
it should "not count empty sub strings" in {
11+
maxScore("00") shouldBe 1
12+
}
13+
14+
it should "work for example case" in {
15+
maxScore("011101") shouldBe 5
16+
}
17+
}

0 commit comments

Comments
 (0)