forked from charles-wangkai/codeforces
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
51 lines (40 loc) · 1.11 KB
/
Main.java
File metadata and controls
51 lines (40 loc) · 1.11 KB
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
45
46
47
48
49
50
51
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
int k = sc.nextInt();
System.out.println(solve(s, k));
sc.close();
}
static String solve(String s, int k) {
int questionCount = (int) s.chars().filter(ch -> ch == '?').count();
int starCount = (int) s.chars().filter(ch -> ch == '*').count();
int minLength = s.length() - (questionCount + starCount) * 2;
if (k < minLength || (starCount == 0 && k > s.length() - questionCount)) {
return "Impossible";
}
int diff = k - minLength;
StringBuilder result = new StringBuilder();
int index = 0;
while (index != s.length()) {
if (index + 1 < s.length() && s.charAt(index + 1) == '*') {
while (diff != 0) {
result.append(s.charAt(index));
diff--;
}
index += 2;
} else if (index + 1 < s.length() && s.charAt(index + 1) == '?') {
if (diff != 0) {
result.append(s.charAt(index));
diff--;
}
index += 2;
} else {
result.append(s.charAt(index));
index++;
}
}
return result.toString();
}
}