-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.kt
48 lines (45 loc) · 1.49 KB
/
solution.kt
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
fun main(args: Array<out String>) {
System.setOut(java.io.PrintStream(System.`out`, false, "UTF-8"))
val s = readLine()!!
var t = readLine()!!
fun findAnswer(isReversed: Boolean, chars: String): Boolean {
if (s.length > chars.length) {
return false
}
if (s.length == chars.length) {
if (isReversed) {
return s.reversed() == chars
} else {
return s == chars
}
}
return if (isReversed) {
listOfNotNull(
if (chars.get(0) == 'A') {
findAnswer(isReversed, chars.substring(1, chars.length))
} else {
null
},
if (chars.get(chars.length - 1) == 'B') {
findAnswer(!isReversed, chars.substring(0, chars.length - 1))
} else {
null
}
)
} else {
listOfNotNull(
if (chars.get(chars.length - 1) == 'A') {
findAnswer(isReversed, chars.substring(0, chars.length - 1))
} else {
null
},
if (chars.get(0) == 'B') {
findAnswer(!isReversed, chars.substring(1, chars.length))
} else {
null
}
)
}.fold(false) { acc, ans -> acc || ans }
}
println(if (findAnswer(false, t)) 1 else 0)
}