# Smallest Number With No Adjacent Duplicates - Google Top Interview Questions

### Problem Statement :

```You are given a string s containing "1", "2", "3" and "?". Given that you can replace any “?” with "1", "2" or "3", return the smallest number you can make as a string such that no two adjacent digits are the same.

Constraints

n ≤ 100,000 where n is the length of s

Example 1

Input

s = "3?2??"

Output

"31212"

Example 2

Input

s = "???"

Output

"121"```

### Solution :

```                        ```Solution in C++ :

string solve(string s) {
for (int i = 0; i < s.size(); i++) {
if (s[i] == '?') {
bool one = true, two = true;
if (i > 0) {
if (s[i - 1] - '0' == 1) one = false;
if (s[i - 1] - '0' == 2) two = false;
}
if (i < s.size() - 1) {
if (s[i + 1] - '0' == 1) one = false;
if (s[i + 1] - '0' == 2) two = false;
}
if (one) {
s[i] = '1';
} else if (two)
s[i] = '2';
else
s[i] = '3';
}
}
return s;
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
public String solve(String s) {
char[] c = s.toCharArray();
int N = s.length();
for (int i = 0; i < N; i++) {
if (c[i] != '?')
continue;
boolean[] b = new boolean;
if (i > 0)
b[c[i - 1] - '1'] = true;
if (i < N - 1 && c[i + 1] != '?')
b[c[i + 1] - '1'] = true;
for (int j = 0; j < 3; j++) {
if (!b[j]) {
c[i] = (char) (j + '1');
break;
}
}
}
return new String(c);
}
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, s):
A = list(s)
for i, c in enumerate(A):
if c == "?":
for d in "123":
A[i] = d
if i - 1 >= 0 and A[i] == A[i - 1]:
continue
if i + 1 < len(A) and A[i] == A[i + 1]:
continue
break

return "".join(A)```
```

## AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

## Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

## Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

## Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

## Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

## Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr