File tree 1 file changed +35
-0
lines changed
TopCoder/SRMs/SRM633/Div2_500_ABBA
1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .HashMap ;
2
+
3
+ public class ABBA {
4
+ private HashMap <String , Boolean > map = new HashMap <String , Boolean >();
5
+
6
+ public String canObtain (String initial , String target ) {
7
+ if (isPossible (initial , target ))
8
+ return "Possible" ;
9
+ return "Impossible" ;
10
+ }
11
+
12
+ private boolean isPossible (String initial , String target ) {
13
+ if (map .containsKey (target )) {
14
+ return map .get (target );
15
+ }
16
+ if (initial .equals (target )) {
17
+ map .put (target , true );
18
+ } else if (initial .length () >= target .length ()) {
19
+ map .put (target , false );
20
+ } else {
21
+ boolean b = false ;
22
+ if (target .endsWith ("A" )) {
23
+ b |= isPossible (initial ,
24
+ target .substring (0 , target .length () - 1 ));
25
+ } else {
26
+ b |= isPossible (
27
+ initial ,
28
+ new StringBuilder (target .substring (0 ,
29
+ target .length () - 1 )).reverse ().toString ());
30
+ }
31
+ map .put (target , b );
32
+ }
33
+ return map .get (target );
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments