# Zuma Game in C++

C++Server Side ProgrammingProgramming

Let us consider about the Zuma Game. Suppose we have a row of balls on the table, these balls are colored as red(R), yellow(Y), blue(B), green(G), and white(W). We also have several balls with us.

Now, each time, we may choose a ball from our side, and insert it into the row. Then, if there is a group of 3 or more balls in the same color touching, remove them. Keep doing this until no more balls can be removed.

We have to find the minimal balls we have to insert to remove all the balls on the table. If we cannot remove all the balls, then return -1.

So if the input is like “WRRBBW”, and we have “RBW”, then the answer will be 3. We can add R after RR, (WRR[R]BBW), after removal, the sequence will be (WBBW), then add B, so (WBB[B]W), after removal it will be (WW), then add W, so the sequence will be (WW[W]). This will remove all balls.

To solve this, we will follow these steps −

• Define a function findMinStep(), this will take s, hand,
• s := s concatenate "#"
• Define an array v of size 26
• for initialize i := 0, when i < size of hand, update (increase i by 1), do −
• increase v[hand[i] - 'A'] by 1
• ret := call the function solve(s, v)
• return ret >= if INF is non zero then check with - 1 otherwise with ret
• Define a function solve(), this will take s, an array v,
• if s is same as "#", then −
• return 0
• ret := INF
• for initialize i := 0, j := 0, when j < size of s, update (increase j by 1), do −
• if s[i] is same as s[j], then −
• need := 3 - (j - i)
• x := s[i]
• if need <= v[x - 'A'], then −
• v[x - 'A'] = v[x - 'A'] - need
• ret := minimum of ret and need + solve(substring of s from 0 to ith index) concatenate substring of s from j to size of s – j, v
• v[x - 'A'] = v[x - 'A'] + need
• i := j
• call the function process(s)
• if s is same as "#", then −
• return 0
• for initialize i := 0, j := 0, when j < size of s, update (increase j by 1), do −
• if s[i] is same as s[j], then −
• need := 3 - (j - i)
• x := s[i]
• if need <= v[x - 'A'], then −
• v[x - 'A'] = v[x - 'A'] - need
• ret := minimum of ret and need + solve(substring of s from 0 to ith index) concatenate substring of s from j to size of s – j, v
• v[x - 'A'] = v[x - 'A'] + need
• i := j
• return ret
• Define a function process(), this will take s,
• for initialize i := 0, j := 0, when j < size of s, update (increase j by 1), do −
• if s[i] is same as s[j], then −
• if (j - i) >= 3, then −
• delete i, j - i from s
• j := i - 1
• Otherwise
• i := j
• Call the findMinStep() with two strings to get the answer

Let us see the following implementation to get better understanding −

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
class Solution {
public:
int findMinStep(string s, string hand) {
s += "#";
vector <int> v(26);
for(int i = 0; i < hand.size(); i++){
v[hand[i] - 'A']++;
}
int ret = solve(s, v);
return ret >= INF ? -1 : ret;
}
int solve(string s, vector <int>& v){
if(s == "#") return 0;
int ret = INF;
for(int i = 0, j = 0; j < s.size(); j++){
if(s[i] == s[j]) continue;
int need = 3 - (j - i);
char x = s[i];
if(need <= v[x - 'A']){
v[x - 'A'] -= need;
ret = min( ret, need + solve(s.substr(0,i) + s.substr(j , s.size() - j), v));
v[x - 'A'] += need;
}
i = j;
}
process(s);
if(s == "#") return 0;
for(int i = 0, j = 0; j < s.size(); j++){
if(s[i] == s[j]) continue;
int need = 3 - (j - i);
char x = s[i];
if(need <= v[x - 'A']){
v[x - 'A'] -= need;
ret = min( ret, need + solve(s.substr(0,i) + s.substr(j , s.size() - j), v));
v[x - 'A'] += need;
}
i = j;
}
return ret;
}
void process(string& s){
for(int i = 0, j = 0; j < s.size(); j++){
if(s[i] == s[j]) continue;
if((j - i) >= 3){
s.erase(i, j - i);
j = i - 1;
} else i = j;
}
}
};
main(){
Solution ob;
cout << (ob.findMinStep("WRRBBW", "RBW"));
}

## Input

"WRRBBW", "RBW"

## Output

3