forked from javadev/LeetCode-in-Kotlin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.kt
41 lines (38 loc) · 1.22 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
package g0001_0100.s0010_regular_expression_matching
// #Hard #Top_Interview_Questions #String #Dynamic_Programming #Recursion #Udemy_Dynamic_Programming
// #Big_O_Time_O(m*n)_Space_O(m*n) #2023_07_03_Time_171_ms_(85.26%)_Space_34.6_MB_(94.74%)
class Solution {
fun isMatch(s: String, p: String): Boolean {
val n = s.length
val m = p.length
return solve(n - 1, m - 1, s, p)
}
private fun solve(i: Int, j: Int, s: String, p: String): Boolean {
if (j < 0) {
return i < 0
}
if (i < 0) {
return p[j] == '*' && solve(i, j - 2, s, p)
}
// simple char matching
// if s char matchs with p char or it can be '.'
if (s[i] == p[j] || p[j] == '.') {
return solve(i - 1, j - 1, s, p)
}
return if (p[j] == '*') {
// if s char matches with p char or it can be '.'
if (s[i] == p[j - 1] || p[j - 1] == '.') {
solve(i - 1, j, s, p) || solve(i, j - 2, s, p)
} else {
solve(
i,
j - 2,
s,
p
)
}
} else {
false
}
}
}