From 95c58e0ea34a1f722bc0c937e1a8c4d09c49138c Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 30 Jul 2024 11:30:23 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1653.=20Minimum=20Deletions?= =?UTF-8?q?=20to=20Make=20String=20Balanced=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/minimum-deletions-to-make-string-balanced.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 go/minimum-deletions-to-make-string-balanced.go diff --git a/go/minimum-deletions-to-make-string-balanced.go b/go/minimum-deletions-to-make-string-balanced.go new file mode 100644 index 0000000..4a1a4e3 --- /dev/null +++ b/go/minimum-deletions-to-make-string-balanced.go @@ -0,0 +1,17 @@ +package main + +func minimumDeletions(s string) int { + dp := make([]int, len(s)+1) + + bs := 0 + for i, c := range s { + if c == 'b' { + dp[i+1] = dp[i] + bs++ + } else { + dp[i+1] = min(dp[i]+1, bs) + } + } + + return dp[len(s)] +}