Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 331
Good morning! Here's your coding interview problem for today.

This problem was asked by LinkedIn.

You are given a string consisting of the letters `x` and `y`, such as `xyxxxyxyy`. In addition, you have an operation called flip, which changes a single `x` to `y` or vice versa.

Determine how many times you would need to apply this operation to ensure that all `x`'s come before all `y`'s. In the preceding example, it suffices to flip the second and sixth characters, so you should return `2`.

Pull request:
Day331
Resolves #666.

Patch:
diff --git a/README.md b/README.md
index eb2a77e..0393895 100644
--- a/README.md
+++ b/README.md
@@ -331,6 +331,7 @@ problems from
 * [Day 324](https://github.com/vaskoz/dailycodingproblem-go/issues/658)
 * [Day 325](https://github.com/vaskoz/dailycodingproblem-go/issues/660)
 * [Day 326](https://github.com/vaskoz/dailycodingproblem-go/issues/661)
+* [Day 331](https://github.com/vaskoz/dailycodingproblem-go/issues/666)
 * [Day 332](https://github.com/vaskoz/dailycodingproblem-go/issues/669)
 * [Day 333](https://github.com/vaskoz/dailycodingproblem-go/issues/668)
 * [Day 337](https://github.com/vaskoz/dailycodingproblem-go/issues/674)
@@ -356,3 +357,4 @@ problems from
 * [Day 363](https://github.com/vaskoz/dailycodingproblem-go/issues/716)
 * [Day 364](https://github.com/vaskoz/dailycodingproblem-go/issues/718)
 * [Day 365](https://github.com/vaskoz/dailycodingproblem-go/issues/720)
+
diff --git a/day331/problem.go b/day331/problem.go
new file mode 100644
index 0000000..fbcd01d
--- /dev/null
+++ b/day331/problem.go
@@ -0,0 +1,35 @@
+package day331
+
+// FlipsXsBeforeYs returns the minimum number of flips
+// required to meet the condition that the result should
+// have all X's before Y's.
+// Runs in O(N) time and O(1) space.
+func FlipsXsBeforeYs(input string) int {
+	streakX, streakY := true, true
+
+	var changeXtoY, changeYtoX int
+
+	for forward, backward := 0, len(input)-1; forward < len(input); forward, backward = forward+1, backward-1 {
+		if streakX && input[forward] != 'x' {
+			streakX = false
+		}
+
+		if streakY && input[backward] != 'y' {
+			streakY = false
+		}
+
+		if !streakX && input[forward] == 'x' {
+			changeXtoY++
+		}
+
+		if !streakY && input[backward] == 'y' {
+			changeYtoX++
+		}
+	}
+
+	if changeXtoY < changeYtoX {
+		return changeXtoY
+	}
+
+	return changeYtoX
+}


Test patch:
diff --git a/day331/problem_test.go b/day331/problem_test.go
new file mode 100644
index 0000000..757898d
--- /dev/null
+++ b/day331/problem_test.go
@@ -0,0 +1,38 @@
+package day331
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	input string
+	flips int
+}{
+	{"xyxxxyxyy", 2},
+	{"yxxyyyy", 1},
+	{"yyyxy", 1},
+	{"yxyxyx", 3},
+	{"yyyyxyyyxyyyyxx", 4},
+	{"xxxxxxx", 0},
+	{"yyyyyyyyyy", 0},
+	{"xyyyyyyyy", 0},
+	{"xxxxxxxxyyy", 0},
+	{"xxxyyxxyyxxyyyyyyyyy", 4},
+}
+
+func TestFlipsXsBeforeYs(t *testing.T) {
+	t.Parallel()
+
+	for _, tc := range testcases {
+		if flips := FlipsXsBeforeYs(tc.input); flips != tc.flips {
+			t.Errorf("Expected %v, got %v", tc.flips, flips)
+		}
+	}
+}
+
+func BenchmarkFlipsXsBeforeYs(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			FlipsXsBeforeYs(tc.input)
+		}
+	}
+}