Similar Problems

Similar Problems not available

Zuma Game - Leetcode Solution

Companies:

LeetCode:  Zuma Game Leetcode Solution

Difficulty: Hard

Topics: breadth-first-search string stack dynamic-programming  

Problem Statement:

You are given a string s and an integer k. You can remove at most k adjacent characters from s at once.

Return the final string after all such removals have been made.

If at any point there are less than k adjacent characters remaining in s you should stop.

Example:

Input: s = "abbccc", k = 2 Output: "acc" Explanation: Remove "bb" to get "acccc". Remove "cc" to get "ac".

Solution:

Approach:

The given problem can be solved using a stack data structure. We can use a stack to remove adjacent characters in the string s. If we find k adjacent characters, we remove them from the stack. We repeat this process until there are less than k adjacent characters remaining in s. At each step, we need to check if the top k characters in the stack are the same. If they are, we remove them from the stack.

Algorithm:

  1. Initialize an empty stack as S.
  2. For each character c in the string s: a. If the stack S is empty, push the character c onto the stack. b. Otherwise, if the top k characters of the stack S are the same as c, remove the top k characters from the stack S. c. Otherwise, push the character c onto the stack S.
  3. Create an empty string as result.
  4. While the stack S is not empty, remove the top character c from the stack S and append it to the string result.
  5. Return the string result.

Code:

class Solution { public: string removeDuplicates(string s, int k) { stack<pair<char, int>> st; for (int i = 0; i < s.size(); i++) { if (st.empty() || st.top().first != s[i]) { st.push(make_pair(s[i], 1)); } else { st.top().second++; if (st.top().second == k) { st.pop(); } } } string result = ""; while (!st.empty()) { pair<char, int> p = st.top(); st.pop(); for (int i = 0; i < p.second; i++) { result += p.first; } } reverse(result.begin(), result.end()); return result; } };

Time Complexity:

The time complexity of this algorithm is O(n) where n is the length of the input string s.

Space Complexity:

The space complexity of this algorithm is O(n) where n is the length of the input string s.

Zuma Game Solution Code

1The basic idea is to use BFS to find the shortest path to the target. We start from the initial state, and enqueue all possible moves. For each move, we update the board and check if the target is reached. If not, we enqueue all possible moves from the updated state and continue.
2
3We can keep track of the number of moves needed to reach the target, and return -1 if the target is not reachable.
4
5class Solution {
6public:
7    int findMinStep(string board, string hand) {
8        unordered_map<char, int> counts;
9        for (char c : hand) {
10            counts[c]++;
11        }
12        int res = bfs(board, counts);
13        return res == INT_MAX ? -1 : res;
14    }
15    
16    int bfs(string board, unordered_map<char, int>& counts) {
17        queue<string> q;
18        queue<int> steps;
19        unordered_set<string> visited;
20        q.push(board);
21        steps.push(0);
22        visited.insert(board);
23        while (!q.empty()) {
24            string s = q.front();
25            q.pop();
26            int step = steps.front();
27            steps.pop();
28            if (isComplete(s)) {
29                return step;
30            }
31            for (int i = 0, n = s.size(); i < n; i++) {
32                int j = i;
33                while (j < n && s[j] == s[i]) j++;
34                int need = 3 - (j - i);
35                if (counts[s[i]] >= need) {
36                    string t = remove(s.substr(0, i) + s.substr(j));
37                    if (visited.find(t) == visited.end()) {
38                        q.push(t);
39                        steps.push(step + need);
40                        visited.insert(t);
41                    }
42                }
43                i = j - 1;
44            }
45        }
46        return INT_MAX;
47    }
48    
49    bool isComplete(string board) {
50        for (int i = 0, n = board.size(); i < n; i++) {
51            int j = i;
52            while (j < n && board[j] == board[i]) j++;
53            if (j - i >= 3) {
54                return false;
55            }
56            i = j - 1;
57        }
58        return true;
59    }
60    
61    string remove(string s) {
62        for (int i = 0, n = s.size(); i < n; i++) {
63            int j = i;
64            while (j < n && s[j] == s[i]) j++;
65            if (j - i >= 3) {
66                return remove(s.substr(0, i) + s.substr(j));
67            }
68            i = j - 1;
69        }
70        return s;
71    }
72};