File tree Expand file tree Collapse file tree 1 file changed +70
-0
lines changed
Expand file tree Collapse file tree 1 file changed +70
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments