-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmallestNumberWithNoAdjacentDuplicates.java
44 lines (40 loc) · 1.33 KB
/
SmallestNumberWithNoAdjacentDuplicates.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class SmallestNumberWithNoAdjacentDuplicates {
/*
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.
*/
private static String fun(String s){
int n;
n = s.length();
char ch[] = s.toCharArray();
for(int i=0;i<n;i++){
if(ch[i] != '?'){
}
else{
int can = 1;
while(true){
if(i>0 && (ch[i-1]-'0') == can){
System.out.println("in if");
can++;
continue;
}
System.out.println("in else"+" "+(i+1));
if((i+1) < ch.length && (ch[i+1]-'0') == can){
System.out.println("in se if");
can++;
continue;
}
break;
}
ch[i] = (char)(can+'0');
}
}
return new String(ch);
}
public static void main(String[] args) {
String s = "3?2??";
System.out.println(fun(s));
}
}