Skip to content

Commit a80bd84

Browse files
author
Thuy Trinh
committed
Solution for "Decode Ways"
1 parent 03986a7 commit a80bd84

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
https://leetcode.com/problems/decode-ways/description/
2+
3+
A -> 1
4+
B -> 2
5+
C -> 3
6+
D -> 4
7+
E -> 5
8+
F -> 6
9+
...
10+
Z -> 26
11+
12+
Some special cases:
13+
* 0 -> 0 way.
14+
* 01 -> 0 way.
15+
* 10 -> 1 way.
16+
* 20 -> 1 way.
17+
* 50 -> 0 way.
18+
* 150 -> 0 way.
19+
20+
"12" -> AB | L -> 2 ways
21+
"126" -> ABF | AZ | LF -> 3 ways
22+
23+
E.g. "12":
24+
Given f("12") which returns the total number ways to decode "12".
25+
* If we choose 1, we have one way to decode:
26+
* 1 -> A
27+
* 2 -> B
28+
* If we choose 12, we have one way to decode: 12 -> L.
29+
* Thus, the total number of ways to decode: 1 + 1 -> 2.
30+
31+
E.g. "123":
32+
* If choosing 1 -> A, then we'll have to decode 23. There're 2 ways to decode 23. So the number of ways to decode is 2.
33+
* If choosing 12 -> L, then we'll decode 3 and we got only one way to do this. So the number of ways to decode is 1.
34+
* Thus, the total number of ways to decode is 2 + 1 -> 3.
35+
36+
E.g. "1234":
37+
* 1,2,3,4
38+
* 12,3,4
39+
* 1,23,4
40+
-> 3 ways?
41+
42+
d(s, i): Returns the number of ways to decode s.
43+
* i > s.lastIndex -> 0
44+
* Choose s[i] -> r += max(1, d(s, i + 1))
45+
* If s[i] and s[i + 1] can be decoded into an alphabet char, choose s[i] and s[i + 1] -> r += max(1, d(s, i + 2))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package leetcode.decodeways
2+
3+
/**
4+
* https://leetcode.com/problems/decode-ways/description/
5+
*/
6+
class Solution {
7+
fun numDecodings(s: String): Int = when {
8+
s.isEmpty() -> 0
9+
else -> maxOf(decodeWays(s), 0)
10+
}
11+
12+
private fun decodeWays(
13+
s: String,
14+
i: Int = 0,
15+
cache: MutableMap<Int, Int> = mutableMapOf()
16+
): Int = when {
17+
i > s.lastIndex -> 0
18+
s[i] == '0' -> -1
19+
i == s.lastIndex -> 1
20+
else -> cache.getOrPut(i) {
21+
val w1 = decodeWays(s, i + 1, cache)
22+
val w2 = if ("${s[i]}${s[i + 1]}".toInt() <= 26) {
23+
decodeWays(s, i + 2, cache)
24+
} else {
25+
-1
26+
}
27+
when {
28+
w1 == -1 && w2 == -1 -> -1
29+
w1 == -1 -> maxOf(1, w2)
30+
w2 == -1 -> maxOf(1, w1)
31+
else -> maxOf(1, w1) + maxOf(1, w2)
32+
}
33+
}
34+
}
35+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode.decodeways
2+
3+
import org.amshove.kluent.shouldEqualTo
4+
import org.junit.Test
5+
6+
class SolutionTest {
7+
@Test
8+
fun decodeWays() {
9+
Solution().numDecodings("0").shouldEqualTo(0)
10+
Solution().numDecodings("").shouldEqualTo(0)
11+
Solution().numDecodings("2").shouldEqualTo(1)
12+
Solution().numDecodings("12").shouldEqualTo(2)
13+
Solution().numDecodings("123").shouldEqualTo(3)
14+
Solution().numDecodings("1234").shouldEqualTo(3)
15+
Solution().numDecodings("150").shouldEqualTo(0)
16+
Solution().numDecodings("20").shouldEqualTo(1)
17+
Solution().numDecodings("50").shouldEqualTo(0)
18+
}
19+
}

0 commit comments

Comments
 (0)