Skip to content

Commit e155e45

Browse files
authored
[20250810] BOJ / G4 / A를 B로 / 이종환
1 parent f0fa933 commit e155e45

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

0224LJH/202508/10 BOJ A를 B로.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
```java
2+
import java.awt.*;
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
import java.util.List;
8+
9+
10+
public class Main {
11+
static String target,goal;
12+
13+
static int size,ans;
14+
static int[] arr;
15+
16+
static List<Character> list = new ArrayList<>();
17+
18+
public static void main(String[] args) throws IOException {
19+
init();
20+
process();
21+
print();
22+
}
23+
24+
private static void init() throws IOException {
25+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
26+
target = br.readLine();
27+
goal = br.readLine();
28+
ans = 0;
29+
}
30+
31+
private static void process() throws IOException {
32+
33+
int goalIdx = goal.length()-1;
34+
35+
for (int i = target.length()-1; i >= 0; i--) {
36+
if (target.charAt(i) == goal.charAt(goalIdx)) {
37+
goalIdx--;
38+
} else {
39+
list.add(target.charAt(i));
40+
ans++;
41+
}
42+
}
43+
44+
if (goalIdx == -1) return;
45+
46+
Outer:
47+
while (goalIdx >= 0) {
48+
for (int i = 0; i < list.size(); i++) {
49+
Character c = list.get(i);
50+
if (c==goal.charAt(goalIdx)) {
51+
goalIdx--;
52+
list.remove(i);
53+
continue Outer;
54+
}
55+
}
56+
57+
goalIdx--;
58+
59+
}
60+
61+
if (list.size() != 0) ans = -1;
62+
63+
}
64+
65+
66+
private static void print() {
67+
System.out.println(ans);
68+
}
69+
}
70+
```

0 commit comments

Comments
 (0)