Skip to content

Commit 72ee166

Browse files
committed
Add: SRM 633 Div2 Medium
1 parent 531d96d commit 72ee166

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Diff for: TopCoder/SRMs/SRM633/Div2_500_ABBA/ABBA.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)