forked from oleg-cherednik/DailyCodingProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
37 lines (31 loc) · 1.24 KB
/
Solution.java
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
/**
* @author Oleg Cherednik
* @since 29.11.2020
*/
public class Solution {
public static void main(String... args) {
System.out.println(isMatches("abcdefg", "ab*ef.")); // true
System.out.println(isMatches("abcdefg", ".***.e*")); // true
System.out.println(isMatches("ray", "ra.")); // true
System.out.println(isMatches("chats", "ra.")); // false
System.out.println(isMatches("chats", "chat.")); // true
}
public static boolean isMatches(String str, String regexp) {
return isMatches(str, 0, regexp, 0);
}
private static boolean isMatches(String str, int i, String regexp, int j) {
if (j >= regexp.length())
return i >= str.length();
if (regexp.charAt(j) == '*') {
if (j + 1 == regexp.length())
return true;
if (isMatches(str, i, regexp, j + 1))
return true;
for (int k = 1; k < str.length(); k++)
if (isMatches(str, i + k, regexp, j + 1))
return true;
} else if (str.charAt(i) == regexp.charAt(j) || regexp.charAt(j) == '.')
return isMatches(str, i + 1, regexp, j + 1);
return false;
}
}